mp3播放器

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.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//歌词显示,每次用bufferreader阅读一行,把[]里面的解析成时间.map 里面的key为时间.value为歌词
//在音乐播放的时候,定义一个计时器,如果时间为map里面的key,就显示value.
public class DemoActivity extends Activity implements OnClickListener {
	private EditText et_path;
	private Button bt_play, bt_pause, bt_replay, bt_stop;
	private MediaPlayer mediaPlayer;
	private TelephonyManager manager ;
	
//MediaPlayer 媒体播放器
	@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);
		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);
	}

	public void onClick(View v) {
		String path;
		try {
			switch (v.getId()) {

			case R.id.play:
			//得到文件的路径
				path = et_path.getText().toString().trim();
				//play方法在下面
				play(path);
				break;
			case R.id.pause:
			//暂停音乐播放器 .pause方法在下面.
				pause();

				break;
			case R.id.replay:
				if (mediaPlayer != null && mediaPlayer.isPlaying()) {
				// seekto文件头.
					mediaPlayer.seekTo(0);
				} else {
				//若果文件为空的话,就开启播放.
					path = et_path.getText().toString().trim();
					play(path);
				}

				break;
			case R.id.stop:
				if (mediaPlayer != null && mediaPlayer.isPlaying()) {
					mediaPlayer.stop();
					mediaPlayer = null;
					bt_play.setEnabled(true);
					bt_play.setClickable(true);
				}

				break;

			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "文件播放出现异常", 0).show();
		}

	}

	private void pause() {
		// 判断音乐是否在播放
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			// 暂停音乐播放器
			mediaPlayer.pause();
			bt_pause.setText("续播");
			return;
		}
		if (mediaPlayer != null
				&& "续播".equals(bt_pause.getText().toString())) {
			mediaPlayer.start();
			bt_pause.setText("暂停");
		}
	}

	/**
	 * 播放指定地址的音乐文件 .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);
						
					}
				});
				
				
//			}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;

			}
		}
		
	}
}

你可能感兴趣的:(mp3播放器)