点击第一个按钮,播放音乐,第二个按钮,暂停或播放,第三个按钮停止播放
1.MainActivity
public class MainActivity extends Activity {
private MediaPlayer mPlayer;
private boolean mIsPause;
private File mFile;
private TextView mHint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
mHint = (TextView) findViewById(R.id.textview1);
mFile = new File("/sdcard/KuwoMusic/music/小路-纯音乐.mp3");
if (mFile.exists()) {
mPlayer = MediaPlayer.create(this,
Uri.parse(mFile.getAbsolutePath()));
} else {
mHint.setText("要播放的音频文件不存在!");
button1.setEnabled(false);
return;
}
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play();
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
play();
if (mIsPause) {
button2.setText("暂停");
mIsPause = false;
}
button2.setEnabled(true);
button3.setEnabled(true);
button1.setEnabled(false);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mPlayer.isPlaying() && !mIsPause) {
mPlayer.pause();
mIsPause = true;
((Button) v).setText("继续");
// button2.setText("继续");
mHint.setText("暂停播放音频...");
button1.setEnabled(true);
} else {
mPlayer.start();
((Button) v).setText("暂停");
// button2.setText("暂停");
mHint.setText("继续播放音频...");
mIsPause = false;
button1.setEnabled(false);
}
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.stop();
mHint.setText("停止播放音频...");
button2.setEnabled(false);
button3.setEnabled(false);
button1.setEnabled(true);
}
});
}
private void play() {
mPlayer.reset();
try {
mPlayer.setDataSource(mFile.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
mHint.setText("正在播放音频...");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
}
@Override
protected void onDestroy() {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.release();
super.onDestroy();
}
}
2.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
/>
</LinearLayout>
</LinearLayout>