Android 使用Service实现简易音乐播放器

1. Service 部分

package com.jereh.learnbymyself.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.jereh.learnbymyself.R;

/**
* Created by Hh on 2017/1/22.
*/

public class MusicService extends Service {

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

    private MediaPlayer mediaPlayer;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            String action = intent.getStringExtra("action");
            if (action.equals("play")) {
                play();
            }
            if (action.equals("pause")) {
                pause();
            }
            if (action.equals("stop")) {
                stop();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void play() {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.monster);
        }
        mediaPlayer.start();
    }

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

    private void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    public void onDestroy() {
        stop();
        super.onDestroy();
    }
}

2. 界面部分

package com.jereh.learnbymyself.service;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.jereh.learnbymyself.R;

public class MediaActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);
        findViewById(R.id.play).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.exit).setOnClickListener(this);
    }

    private MediaPlayer mediaPlayer;

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MusicService.class);
        switch (v.getId()) {
            case R.id.play:
                intent.putExtra("action", "play");
                startService(intent);
                break;
            case R.id.pause:
                intent.putExtra("action", "pause");
                startService(intent);
                break;
            case R.id.stop:
                intent.putExtra("action", "stop");
                startService(intent);
                break;
            case R.id.exit:
                intent.putExtra("action", "exit");
                stopService(intent);
                finish();
                break;
        }
    }

}


你可能感兴趣的:(Android)