import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class Ex06Activity extends Activity {
privateImageButton mb1, mb2, mb3;
privateTextView tv;
privateMediaPlayer mp;
//声明一个变量判断是否为暂停,默认为false
privateboolean isPaused = false;
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 通过findViewById找到资源
mb1 = (ImageButton)findViewById(R.id.imageButton1);
mb2 = (ImageButton)findViewById(R.id.imageButton2);
mb3 = (ImageButton)findViewById(R.id.imageButton3);
tv = (TextView)findViewById(R.id.textView);
// 创建MediaPlayer对象,将raw文件夹下的fighter.mp3
mp = MediaPlayer.create(this,R.raw.fighter);
// 增加播放音乐按钮的事件
mb1.setOnClickListener(newImageButton.OnClickListener() {

@Override
public void onClick(View v) {
try {
if (mp != null) {
mp.stop();
}
mp.prepare();
mp.start();
tv.setText("音乐播放中...");
} catch (Exception e) {
tv.setText("播放发生异常...");
e.printStackTrace();
}
}
});
mb2.setOnClickListener(newImageButton.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mp != null) {
mp.pause();
tv.setText("音乐停止播放...");
}
} catch (Exception e) {
tv.setText("音乐停止发生异常...");
e.printStackTrace();
}
}
});
mb3.setOnClickListener(newImageButton.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mp != null) {
if (isPaused == false) {
mp.pause();
//定位为开始
mp.seekTo(0);
isPaused = true;
tv.setText("停止播放!");
} else if (isPaused == true) {
mp.start();
isPaused = false;
tv.setText("开始播发!");
}
}
} catch (Exception e) {
tv.setText("发生异常...");
e.printStackTrace();
}
}
});
mp.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
// @Override
public void onCompletion(MediaPlayer arg0){
try {
mp.release();
tv.setText("音乐播发结束!");
} catch (Exception e) {
tv.setText(e.toString());
e.printStackTrace();
}
}
});
mp.setOnErrorListener(newMediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer arg0, intarg1, int arg2) {
// TODO Auto-generated method stub
try {
mp.release();
tv.setText("播放发生异常!");
} catch (Exception e) {
tv.setText(e.toString());
e.printStackTrace();
}
return false;
}
});
}
}
main.xml:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="音乐播放" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.81" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/start" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop" />
</LinearLayout>
</LinearLayout>
注意:
1、fighter.mp3放置于res目录下raw文件夹下。不能有大写。
2、MediaPlayer.create(this,R.raw.fighter);直接将这个mp3import到了项目中,此时,stop()和pause()都为暂停功能,如果要实现从头开始播放,seekto(0),在pause()后定位为开始,在start()。
3、注意错误处理,错误提醒,多方面判断。