android-----简单的音乐播放器


    
    
        
        
        
        
    
    	
        	

package com.example.mp3player;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnClickListener{
	ActionBar mActionBar;
	Button pause,play,exit;
	ProgressBar mProgressBar;
	static Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mActionBar=getActionBar();
		mActionBar.hide();
		pause=(Button) findViewById(R.id.pause);
		play=(Button) findViewById(R.id.play);
		exit=(Button) findViewById(R.id.exit);
		mProgressBar=(ProgressBar) findViewById(R.id.mProgressBar);
		
		pause.setOnClickListener(this);
		play.setOnClickListener(this);
		exit.setOnClickListener(this);
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				Bundle mBundle=new Bundle();
				mBundle=msg.getData();
				mProgressBar.setProgress(mBundle.getInt("progress",0));
		    }
		};
		
	}

	@Override
	public void onClick(View v) {
		int p=0;
		Intent intent=new Intent("com.example.mp3player.musicService");
		
		if(v==play)
			p=1;
		else if(v==pause)
			p=2;
		else if(v==exit)
		{
			stopService(intent);
			this.finish();
		}
		
		Bundle bundle=new Bundle();
		bundle.putInt("op", p);
		intent.putExtras(bundle);
		
		startService(intent);
	}

}

package com.example.mp3player;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;

public class MusicService extends Service {
	private MediaPlayer mediaPlayer=null;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate(){
		if(mediaPlayer==null)
		{
			mediaPlayer=MediaPlayer.create(this, R.raw.pin);
			mediaPlayer.setLooping(true);
		}
		
	}
	
	
	@Override
	public void onDestroy(){
		if(mediaPlayer!=null)
		{
			mediaPlayer.pause();
			mediaPlayer.stop();
			mediaPlayer.release();
		}
	}
	
	@Override
	public void onStart(Intent intent,int id){
		if(intent==null) return ;
		int p=intent.getIntExtra("op", 1);
		
		switch(p)
		{
			case 1:
				play();break;
			case 2:
				pause();break;
		}
	}
	
	public void play(){
		if(!mediaPlayer.isPlaying())
		{
			mediaPlayer.start();
			
			Thread thread=new Thread(new Runnable(){
				@Override
				public void run() {
					while(mediaPlayer!=null&&mediaPlayer.isPlaying())
					{
						Bundle bundle=new Bundle();
						bundle.putInt("progress", mediaPlayer.getCurrentPosition()*100/mediaPlayer.getDuration());
						Message message=new Message();
						message.setData(bundle);
						MainActivity.handler.sendMessage(message);
					}
				}});
			thread.start();
		}
			

	}
	
	public void pause(){
		if(mediaPlayer.isPlaying())
		{
			mediaPlayer.pause();
		}
			

	}
	
	

}

你可能感兴趣的:(android)