1. 程式人生 > >利用Service播放音樂

利用Service播放音樂


public class MusicService extends Service {

    private MediaPlayer mediaPlayer;

    private OperatorReceiver mReceier;

    public MusicService() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mReceier=new OperatorReceiver();
        IntentFilter filter=new IntentFilter("operator.receiver");
        registerReceiver(mReceier,filter);

        mediaPlayer=new MediaPlayer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        ready();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy()
    {

        if(mediaPlayer!=null&&mediaPlayer.isPlaying())
        {
            mediaPlayer.stop();
        }

        if (mediaPlayer!=null)
        {
            mediaPlayer.release();
            mediaPlayer=null;
        }

        unregisterReceiver(mReceier);

        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
    }

    private void start()
    {
        mediaPlayer.start();
    }

    private  void pause()
    {
        mediaPlayer.pause();
    }

    private void stop()
    {
        mediaPlayer.stop();
        ready();
    }

    public class Tools {

        public static final int PLAYER=1;
        public static  final int PAUSE=2;
        public static final int STOP=3;
    }

    private void  ready()
    {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource("/sdcard/dj.mp3");
            mediaPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
        }
    }

    class OperatorReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            int cmd=intent.getIntExtra("cmd",-1);

            switch (cmd)
            {
                case Tools.PLAYER:
                    start();break;
                case Tools.PAUSE:
                    pause();break;
                case Tools.STOP:
                    stop();break;
            }


        }


    }
}
<service android:name=".MusicService"></service>

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this,MusicService.class));
    }

    public void play(View view)
    {
        Intent intent=new Intent("operator.receiver");
        intent.putExtra("cmd", MusicService.Tools.PLAYER);

        sendBroadcast(intent);

    }

    public void pause(View view)
    {
        Intent intent=new Intent("operator.receiver");
        intent.putExtra("cmd", MusicService.Tools.PAUSE);

        sendBroadcast(intent);
    }

    public void stop(View view)
    {
        Intent intent=new Intent("operator.receiver");
        intent.putExtra("cmd", MusicService.Tools.STOP);

        sendBroadcast(intent);
    }


}