用Activity实现音乐播放器,播放存入SD卡的MP3文件。
注意的点:
1.实现播放,暂停,重播,停止:
暂停时按钮变为“继续”,播放后再点击变为“暂停”,
重播时,如果正在播放则重新播放,否则直接播放
2.有电话打进来的时候要暂停播放,通话结束后继续播放。
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="xxx.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/playbutton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause"
android:id="@+id/pausebutton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset"
android:id="@+id/resetbutton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"
android:id="@+id/stopbutton"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private EditText eFileName;
private File audioFile;
private MediaPlayer mediaPlayer;
private int position;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eFileName = (EditText) this.findViewById(R.id.filename);//找到文本框要播放的文件的名称
Button playButton = (Button) this.findViewById(R.id.playbutton);//找到按钮
Button pauseButton = (Button) this.findViewById(R.id.pausebutton);//找到按钮
Button resetButton = (Button) this.findViewById(R.id.resetbutton);//找到按钮
Button stopButton = (Button) this.findViewById(R.id.stopbutton);//找到按钮
ButtonClickListener listener = new ButtonClickListener();
playButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
resetButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);
}
//如果正在播放音乐,有电话打进来了,我们要暂停音乐播放,这时要重写onPause()方法来解决这个问题,
//因为打电话的Activity覆盖了当前MainActivity,而不管覆盖还是半覆盖,都会调用onPause()方法
protected void onPause() {
if(mediaPlayer.isPlaying()){
position = mediaPlayer.getCurrentPosition();//取得当前播放进度,以便于挂断电话后继续播放
mediaPlayer.stop();
}
super.onPause();
}
//挂断电话后要继续播放音乐,重写onResume方法。
//因为不管MainActivity是停止状态还是暂停状态,要回到运行状态必然会调用onResume方法
protected void onResume() {
if(audioFile != null && position >0){
try {
play();
mediaPlayer.seekTo(position);//从onPause中暂停的位置播放
position = 0;//把播放位置重置为0
} catch (IOException e) {
e.printStackTrace();
}
}
super.onResume();
}
//如果电话打进来后,内存不足,把音乐播放器的MainActivity杀死掉,那么要用onSaveInstanceState()方法来保存播放器Activity中的数据
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("position", position);//保存播放进度
if(audioFile!=null){
outState.putString("path", audioFile.getAbsolutePath());//保存播放的音乐文件路径
}
super.onSaveInstanceState(outState);
}
//打电话结束后,重新创建了播放器Activity,调用onRestoreInstanceState方法,
//所以重写该方法,还原杀死播放器时候保存的数据
protected void onRestoreInstanceState(Bundle savedInstanceState) {
position = savedInstanceState.getInt("position");//还原播放进度
String path = savedInstanceState.getString("path");//还原保存的播放文件路径
if(path!=null){
audioFile = new File(path);
}
super.onRestoreInstanceState(savedInstanceState);
}
private void play() throws IOException {
mediaPlayer.reset();//清除,以备多次播放
mediaPlayer.setDataSource(audioFile.getAbsolutePath());//设置要播放的文件的路径
mediaPlayer.prepare();//准备
mediaPlayer.start();//开始播放
}
private final class ButtonClickListener implements View.OnClickListener{
private boolean pause;//记录音乐是否被暂停
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.playbutton://播放
String fileName = eFileName.getText().toString();//得到用户输入的文件名
audioFile = new File(Environment.getExternalStorageDirectory(),fileName);//创建文件对象
mediaPlayer = new MediaPlayer();//初始化媒体播放器
if(audioFile.exists()){
play();
}else{
Toast.makeText(getApplicationContext(), R.string.filenoexsit, 1).show();
audioFile = null;
}
break;
case R.id.pausebutton://暂停
//对音乐的播放状态进行判断,如果是暂停,点击后变为继续,如果是继续,点击后变为暂停
if(mediaPlayer.isPlaying()){//正在播放状态
((Button)v).setText(R.string.goon);
mediaPlayer.pause();
pause = true;
}else{//音乐没播放,这时点击暂停没意义,所以要判断
if(pause){
((Button)v).setText(R.string.pause);
mediaPlayer.start();//继续播放
pause = false;
}
}
break;
case R.id.resetbutton://重播,如果音乐正在播放,则从头播放,否则直接播放
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);//把播放音乐的指针置到开始位置
}else{
if(audioFile !=null){
play();
}
}
break;
case R.id.stopbutton://停止
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}