在Android中可以使用MediaPlayer来播放音频,常见使用方法如下:
MediaPlayer mediaPlayer = new MediaPlayer(); if (mediaPlayer.isPlaying()) { mediaPlayer.reset();//重置为初始状态 } mediaPlayer.setDataSource("/mnt/sdcard/god.mp3"); mediaPlayer.prepare();//缓冲 mediaPlayer.start();//开始或恢复播放 mediaPlayer.pause();//暂停播放 mediaPlayer.start();//恢复播放 mediaPlayer.stop();//停止播放 mediaPlayer.release();//释放资源 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//播出完毕事件 @Override public void onCompletion(MediaPlayer arg0) { mediaPlayer.release(); } }); mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {// 错误处理事件 @Override public boolean onError(MediaPlayer player, int arg1, int arg2) { mediaPlayer.release(); return false; } });下面介绍一个音频播放的简易例子,在这里面除了调用MediaPlayer的API外,还需要处理当播放音乐是遇到来电等事件时的情况,要保证接听完电话后还能继续播放音乐,需要覆写Activity的生命周期的几个方法。
界面:
初始界面
播放过程中,接听电话,会保存当前播放位置,挂断电话后,继续播放
布局文件layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="my life.mp3" android:id="@+id/filename" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" android:id="@+id/play" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause" android:id="@+id/pause" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reset" android:id="@+id/reset" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/stop" /> </LinearLayout> </LinearLayout>
数据文件values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, PlayActivity!</string> <string name="app_name">音乐播放器</string> <string name="filename">音乐文件</string> <string name="play">播放</string> <string name="pause">暂停</string> <string name="reset">重播</string> <string name="stop">停止</string> <string name="continue1">继续</string> </resources>
package com.android.audio; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class AudioPlayerActivity extends Activity { private static final String TAG = "AudioPlayerActivity"; private EditText filenameText; private MediaPlayer mediaPlayer; private String filename; private int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); filenameText = (EditText)this.findViewById(R.id.filename); mediaPlayer = new MediaPlayer(); ButtonClickListener listener = new ButtonClickListener(); Button playButton = (Button)this.findViewById(R.id.play); Button pauseButton = (Button)this.findViewById(R.id.pause); Button resetButton = (Button)this.findViewById(R.id.reset); Button stopButton = (Button) this.findViewById(R.id.stop); playButton.setOnClickListener(listener); pauseButton.setOnClickListener(listener); resetButton.setOnClickListener(listener); stopButton.setOnClickListener(listener); Log.i(TAG, "onCreate()"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { this.filename = savedInstanceState.getString("filename"); this.position = savedInstanceState.getInt("position"); super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState()"); } @Override protected void onSaveInstanceState(Bundle outState) { outState.putString("filename", filename); outState.putInt("position", position); super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState()"); } @Override protected void onPause() {//如果突然电话到来,停止播放音乐 if(mediaPlayer.isPlaying()){ position = mediaPlayer.getCurrentPosition();//保存当前播放点 mediaPlayer.stop(); } super.onPause(); } @Override protected void onResume() { if(position>0 && filename!=null){//如果电话结束,继续播放音乐 try { play(); mediaPlayer.seekTo(position); position = 0; } catch (IOException e) { Log.e(TAG, e.toString()); } } super.onResume(); } @Override protected void onDestroy() { mediaPlayer.release(); super.onDestroy(); Log.i(TAG, "onDestroy()"); } private final class ButtonClickListener implements View.OnClickListener{ @Override public void onClick(View v) { filename = filenameText.getText().toString();//先得到文本框中的内容 Button button = (Button) v;//得到button try { switch (v.getId()) {//通过传过来的Buttonid可以判断Button的类型 case R.id.play://播放 play(); break; case R.id.pause: if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); button.setText(R.string.continue1);//让这个按钮上的文字显示为继续 }else{ mediaPlayer.start();//继续播放 button.setText(R.string.pause); } break; case R.id.reset: if(mediaPlayer.isPlaying()){ mediaPlayer.seekTo(0);//让它从0开始播放 }else{ play();//如果它没有播放,就让它开始播放 } break; case R.id.stop: if(mediaPlayer.isPlaying()) mediaPlayer.stop();//如果它正在播放的话,就让他停止 break; } } catch (Exception e) {//抛出异常 Log.e(TAG, e.toString()); } } } private void play() throws IOException { File audioFile = new File(Environment.getExternalStorageDirectory(),filename); mediaPlayer.reset(); mediaPlayer.setDataSource(audioFile.getAbsolutePath()); mediaPlayer.prepare(); mediaPlayer.start();//播放 } }
开发步骤:
1> 往项目的res/raw目录中放入音效文件。
2> 新建SoundPool对象,然后调用SoundPool.load()加载音效,调用SoundPool.play()方法播放指定音效文件。
public class AudioActivity extends Activity { private SoundPool pool; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //指定声音池的最大音频流数目为10,声音品质为5 pool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5); final int sourceid = pool.load(this, R.raw.pj, 0);//载入音频流,返回在池中的id Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //播放音频,第二个参数为左声道音量;第三个参数为右声道音量;第四个参数为优先级;第五个参数为循环次数,0不循环,-1循环;第六个参数为速率,速率最低0.5最高为2,1代表正常速度 pool.play(sourceid, 1, 1, 0, -1, 1); } }); } }