利用本地Service做简单音乐播放器

在res目录下创建raw文件,来存放音乐

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button btn_play;
    private Button btn_pause;
    private Button btn_stop;
    private Button btn_exit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_play=(Button)findViewById(R.id.play);
        btn_pause=(Button)findViewById(R.id.pause);
        btn_stop=(Button)findViewById(R.id.stop);
        btn_exit=(Button)findViewById(R.id.exit);

        btn_play.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
        
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(this,MusicService.class);
        if(btn_play==v){
            intent.putExtra("action","play");
            startService(intent);

        }else if(btn_pause==v){
            intent.putExtra("action","pause");
            startService(intent);

        }else if(btn_stop==v){
            intent.putExtra("action","stop");
            startService(intent);

        }else if(btn_exit==v){
            intent.putExtra("action","exit");
            startService(intent);

        }
    }
}

Service代码:

public class MusicService extends Service {

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getStringExtra("action");
        if("play".equals(action)){
            play();
        }else if("pause".equals(action)){
            pause();
        }else if("stop".equals(action)){
            stop();
        }else if("exit".equals(action)){
            onDestroy();
        }

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

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

    private MediaPlayer player;

    //播放
    public void play(){
        if(player==null) {
            player = MediaPlayer.create(this, R.raw.hsggx);
        }
        player.start();
    }

    //暂停
    public void pause(){
        if(player!=null&&player.isPlaying()){
            player.pause();

        }
    }
    //停止(重新启动重投开始播)
    public void stop(){
        if(player!=null){
            player.stop();//停止
            player.reset();//重置
            player.release();//释放资源
            player=null;
        }
    }

}

嗯,大概是这样的,就是我们这里使用的是本地Service,还有就是我们用的服务绑定服务,因为如果是绑定的,一关掉activity就会关掉服务,根本不可能后台运行。所以我们用的是一般服务。还有就是一般服务,调用一次过后,每次调用服务只会调用它的onStartCommand方法。所以在这个方法接收activity用Intent带来的数据,之后根据数据内容来判断运行哪个方法

发布了117 篇原创文章 · 获赞 1 · 访问量 7053

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/104436784