音乐播放器

public class MainActivity extends Activity {

	static Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Bundle bundle = msg.getData();
			int duration = bundle.getInt("duration");
			int currentPostition = bundle.getInt("currentPosition");
			//刷新进度条的进度
			sb.setMax(duration);
			sb.setProgress(currentPostition);
		}
	};
	MusicInterface mi;
	private MyserviceConn conn;
	private Intent intent;
	private static SeekBar sb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sb = (SeekBar) findViewById(R.id.sb);
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				//根据拖动的进度改变音乐播放进度
				int progress = seekBar.getProgress();
				//改变播放进度
				mi.seekTo(progress);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		
		
		intent = new Intent(this, MusicService.class);
		startService(intent);
		conn = new MyserviceConn();
		bindService(intent, conn, BIND_AUTO_CREATE);
	}

	class MyserviceConn implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mi = (MusicInterface) service;
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public void play(View v){
		mi.play();
	}
	public void continuePlay(View v){
		mi.continuePlay();
	}
	public void pause(View v){
		mi.pause();
	}
	public void exit(View v){
		unbindService(conn);
		stopService(intent);
	}

}




public class MusicService extends Service {

	MediaPlayer player;
	private Timer timer;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return new MusicController();
	}

	class MusicController extends Binder implements MusicInterface{

		@Override
		public void play() {
			MusicService.this.play();
		}

		@Override
		public void pause() {
			MusicService.this.pause();
		}

		@Override
		public void continuePlay() {
			MusicService.this.continuePlay();
		}

		@Override
		public void seekTo(int progress) {
			MusicService.this.seekTo(progress);
			
		}
		
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		player = new MediaPlayer();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//停止播放
		player.stop();
		//释放占用的资源,此时player对象已经废掉了
		player.release();
		player = null;
		if(timer != null){
			timer.cancel();
			timer = null;
		}
		
	}
	
	//播放音乐
	public void play(){
		//重置
		player.reset();
		try {
			//加载多媒体文件
			player.setDataSource("sdcard/zxmzf.mp3");
//			player.setDataSource("http://192.168.13.119:8080/bzj.mp3");
//			player.prepare();
			player.prepareAsync();
//			player.start();
			player.setOnPreparedListener(new OnPreparedListener() {
				//准备完毕时,此方法调用
				@Override
				public void onPrepared(MediaPlayer mp) {
					player.start();
					addTimer();
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	//继续播放
	public void continuePlay(){
		player.start();
	}
	
	//暂停播放
	public void pause(){
		player.pause();
	}
	
	public void seekTo(int progress){
		player.seekTo(progress);
	}
	public void addTimer(){
		if(timer == null){
			timer = new Timer();
			timer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					//获取歌曲总时长
					int duration = player.getDuration();
					//获取歌曲当前播放进度
					int currentPosition= player.getCurrentPosition();
					
					Message msg = MainActivity.handler.obtainMessage();
					//把进度封装至消息对象中
					Bundle bundle = new Bundle();
					bundle.putInt("duration", duration);
					bundle.putInt("currentPosition", currentPosition);
					msg.setData(bundle);
					MainActivity.handler.sendMessage(msg);
					
				}
				//开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
			}, 5, 500);
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值