采用的Activity来播放音乐
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HomeActivity!</string>
<string name="app_name">音乐播放器</string>
<string name="filename">音乐文件</string>
<string name="play">播放</string>
<string name="pause">暂停</string>
<string name="stop">停止</string>
<string name="replay">重播</string>
</resources>
<?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:id="@+id/filenmae"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/play"
android:text="@string/play"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pause"
android:text="@string/pause"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="@string/stop"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/replay"
android:text="@string/replay"
/>
</LinearLayout>
</LinearLayout>
package com.zyq.mp3;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class HomeActivity extends Activity {
private EditText filename;
private File file;
private MediaPlayer mediaPlayer;
private int position;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mediaPlayer = new MediaPlayer();
filename = (EditText)this.findViewById(R.id.filenmae);
Button play = (Button)this.findViewById(R.id.play);
Button pause = (Button)this.findViewById(R.id.pause);
Button stop = (Button)this.findViewById(R.id.stop);
Button replay = (Button)this.findViewById(R.id.replay);
ButtonListener l = new ButtonListener();
play.setOnClickListener(l);
pause.setOnClickListener(l);
stop.setOnClickListener(l);
replay.setOnClickListener(l);
}
//模拟电话接听,触发Activity onPause生命周期函数
@Override
protected void onPause()
{
if(mediaPlayer.isPlaying())
{
position = mediaPlayer.getCurrentPosition();//保存音乐播放的进度
mediaPlayer.stop();
}
super.onPause();
}
//模拟电话挂断,重新回到音乐播放界面,触发Activity onResume生命周期函数
@Override
protected void onResume()
{
try
{
if(file!=null && position > 0)
{
play();
mediaPlayer.seekTo(position);
}
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onResume();
}
//模拟系统内存不足,KILL掉此Activity,触发的onSaveInstanceState 方法来保存参数
@Override
protected void onSaveInstanceState(Bundle outState)
{
if(file != null)
{
outState.putString("path", file.getAbsolutePath());
}
outState.putInt("position", position);
super.onSaveInstanceState(outState);
}
//模拟系统内存不足,回复此Activity,触发的onRestoreInstanceState 重新设置参数
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
String path = savedInstanceState.getString("path");
if(path != null && "".equals(path))
{
this.file = new File(path);
}
this.position = savedInstanceState.getInt("position");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onDestroy()
{
super.onDestroy();
mediaPlayer.release();
}
private class ButtonListener implements View.OnClickListener
{
private boolean flag;
@Override
public void onClick(View v)
{
try
{
switch (v.getId())
{
case R.id.play:
file = new File(Environment.getExternalStorageDirectory(),filename.getText().toString());
if(file.exists())
{
Toast.makeText(HomeActivity.this, "音乐文件不存在", 1).show();
return;
}
play();
break;
case R.id.pause:
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
flag = true;
((Button)v).setText("继续");
}
else
{
if(flag)
{
mediaPlayer.start();
((Button)v).setText("暂停");
}
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying())
{
mediaPlayer.seekTo(0);
}
else
{
if(file.exists())
{
play();
}
}
break;
case R.id.replay:
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
break;
default:
break;
}
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//播放音乐
private void play() throws IllegalArgumentException, IllegalStateException, IOException
{
mediaPlayer.reset();
mediaPlayer.setDataSource(file.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
}
}