Android 中MediaPlayer播放音频

1、播放/res/raw/目录下的音频资源

public class MainActivity extends Activity {

    MediaPlayer mediaPlayer;
int position = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); } public void start(View v) {
try {
mediaPlayer = MediaPlayer.create(this, R.raw.mus);
if (mediaPlayer != null ) {
mediaPlayer.stop();
position = 0;
}
mediaPlayer.prepare();
if (position > 0) mediaPlayer.seekTo(position);
mediaPlayer.start();
}catch (Exception e) {
e.printStackTrace();
} } public void stop(View v) {
mediaPlayer.stop();
} public void pause(View v) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
} @Override
protected void onDestroy() {
mediaPlayer.release();
super.onDestroy();
} }

2、播放 assets目录下的音频资源

package com.test.mediaplayer;

import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { MediaPlayer mediaPlayer;
int position = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void start(View v) {
try {
mediaPlayer = new MediaPlayer();
AssetManager am = getAssets();
AssetFileDescriptor afd = am.openFd("mus.mp3");
mediaPlayer.setDataSource(afd.getFileDescriptor());
mediaPlayer.prepare();
if (position > 0) mediaPlayer.seekTo(position);
mediaPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void stop(View v) {
mediaPlayer.stop();
} public void pause(View v) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
} @Override
protected void onDestroy() {
mediaPlayer.release();
super.onDestroy();
} }

3、播放sdCard上的音频资源

package com.test.mediaplayer;

import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { MediaPlayer mediaPlayer;
int position = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} public void start(View v) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("/mnt/sdcard/mus.mp3");
mediaPlayer.prepare();
if (position > 0) mediaPlayer.seekTo(position);
mediaPlayer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void stop(View v) {
mediaPlayer.stop();
} public void pause(View v) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
} @Override
protected void onDestroy() {
mediaPlayer.release();
super.onDestroy();
} }

4、播放网络音频资源使用 MediaPlayer的setDataSource(Context context, Uri uri)方法,Uri为音频文件的网络路径。

使用SoundPool来播放密集、短促的音效。MediaPlayer的缺点是:资源占用量高、延迟时间长,不支持多个音频同时播放,SoundPool虽然可以一次性加载多个声音,但是由于内存限制,因此应该避免使用SoundPool来播放歌曲或者做游戏背景。

上一篇:Android 4.4 Kitkat 音频实现及简要分析


下一篇:day12 继承