音乐播放
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;
}
});
音乐播放代码示例:
DemoActivity.java:
package cn.itcast.mp3;
import java.io.File;
import Android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.SystemClock;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.EditText;
import android.widget.Toast;
public class DemoActivity extends Activity implements OnClickListener,
OnChronometerTickListener {
private EditText et_path;
private Chronometer et_time;
private Button bt_play, bt_pause, bt_replay, bt_stop;
private MediaPlayer mediaPlayer;
private TelephonyManager manager;
/**
* subtime:点击“续播”到暂停时的间隔的和 beginTime:重新回到播放时的bash值 falgTime:点击“播放”时的值
* pauseTime:“暂停”时的值
*/
private long subtime = 0, beginTime = 0, falgTime = 0, pauseTime = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
manager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
manager.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
et_path = (EditText) this.findViewById(R.id.et_path);
et_time = (Chronometer) this.findViewById(R.id.et_time);
bt_play = (Button) this.findViewById(R.id.play);
bt_pause = (Button) this.findViewById(R.id.pause);
bt_replay = (Button) this.findViewById(R.id.replay);
bt_stop = (Button) this.findViewById(R.id.stop);
bt_play.setOnClickListener(this);
bt_pause.setOnClickListener(this);
bt_replay.setOnClickListener(this);
bt_stop.setOnClickListener(this);
et_time.setOnChronometerTickListener(this);
}
public void onClick(View v) {
String path;
try {
switch (v.getId()) {
case R.id.play:
falgTime = SystemClock.elapsedRealtime();
path = et_path.getText().toString().trim();
play(path);
pauseTime = 0;
et_time.setBase(falgTime);
et_time.start();
break;
case R.id.pause:
pause();
break;
case R.id.replay:
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);
et_time.setBase(SystemClock.elapsedRealtime());
et_time.start();
} else {
path = et_path.getText().toString().trim();
play(path);
et_time.setBase(SystemClock.elapsedRealtime());
et_time.start();
}
if ("续播".equals(bt_pause.getText().toString().trim())) {
bt_pause.setText("暂停");
}
falgTime = SystemClock.elapsedRealtime();
subtime = 0;
break;
case R.id.stop:
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer = null;
et_time.setBase(SystemClock.elapsedRealtime());
et_time.start();
et_time.stop();
bt_play.setEnabled(true);
bt_play.setClickable(true);
}
falgTime = 0;
subtime = 0;
break;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "文件播放出现异常", 0).show();
}
}
private void pause() {
// 判断音乐是否在播放
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
// 暂停音乐播放器
mediaPlayer.pause();
bt_pause.setText("续播");
et_time.stop();
pauseTime = SystemClock.elapsedRealtime();
System.out.println("1 pauseTime" + pauseTime);
} else if (mediaPlayer != null
&& "续播".equals(bt_pause.getText().toString())) {
subtime += SystemClock.elapsedRealtime() - pauseTime;
System.out.println("2 subtime:" + subtime);
mediaPlayer.start();
bt_pause.setText("暂停");
beginTime = falgTime + subtime;
System.out.println("3 beginTime" + beginTime);
et_time.setBase(beginTime);
et_time.start();
}
}
/**
* 播放指定地址的音乐文件 .mp3 .wav .amr
*
* @param path
*/
private void play(String path) throws Exception {
if ("".equals(path)) {
Toast.makeText(getApplicationContext(), "路径不能为空", 0).show();
return;
}
File file = new File(path);
if (file.exists()) {
// 如果文件存在 才进行播放操作
// if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(path);
// mediaPlayer.prepare(); // c/c++ 播放器引擎的初始化
// 同步方法
// 采用异步的方式
mediaPlayer.prepareAsync();
// 为播放器注册
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
bt_play.setEnabled(false);
bt_play.setClickable(false);
}
});
// 注册播放完毕后的监听事件
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
mediaPlayer = null;
bt_play.setEnabled(true);
bt_play.setClickable(true);
et_time.setBase(SystemClock.elapsedRealtime());
et_time.start();
et_time.stop();
}
});
// }else{
// mediaPlayer.reset(); // 重置 播放器的状态
// mediaPlayer.setDataSource(path);
// mediaPlayer.prepare(); // c/c++ 播放器引擎的初始化
// mediaPlayer.start();
// }
} else {
Toast.makeText(getApplicationContext(), "文件不存在", 0).show();
return;
}
}
private class MyListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// 音乐播放器暂停
pause();
break;
case TelephonyManager.CALL_STATE_IDLE:
// 重新播放音乐
pause();
break;
}
}
}
public void onChronometerTick(Chronometer chronometer) {
}
}