Service后台播放音乐实例

首先展示代码结构图

Service后台播放音乐实例_第1张图片

首先在res里建立raw文件用来存放音频文件 这里注意如果写代码过程中发现raw文件引用不到 就build-clear project 一下

编写main-xml文件




    

main-activity 代码

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_main_play;
    Button btn_main_pause;
    Button btn_main_stop;
    Button btn_main_exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_main_play = findViewById(R.id.btn_main_play);
        btn_main_pause = findViewById(R.id.btn_main_pause);
        btn_main_stop = findViewById(R.id.btn_main_stop);
        btn_main_exit = findViewById(R.id.btn_main_exit);
        btn_main_play.setOnClickListener(this);
        btn_main_pause.setOnClickListener(this);
        btn_main_stop.setOnClickListener(this);
        btn_main_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, MusicService.class);
        switch (view.getId()) {
            case R.id.btn_main_play:
                intent.putExtra("action", "play");
                startService(intent);
                break;
            case R.id.btn_main_pause:
                intent.putExtra("action", "pause");
                startService(intent);
                break;
            case R.id.btn_main_stop:
                intent.putExtra("action", "stop");
                startService(intent);
                break;
            case R.id.btn_main_exit:
                //关闭服务 结束activity
                stopService(intent);
                finish();
                break;
        }
    }
}

新建MusicService 继承Service

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

public class MusicService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getStringExtra("action");
        switch (action) {
            case "play":
                play();
                break;
            case "pause":
                pause();
                break;
            case "stop":
                stop();
                break;
        }
        return super.onStartCommand(intent, flags, startId);

    }

    private MediaPlayer mp;

    private void exit() {
        stop();
    }

    private void stop() {
        if (mp != null) {
            mp.stop();//停止
            mp.reset();//重置
            mp.release();//释放资源
            mp = null;
        }

    }

    private void pause() {
        if (mp != null && mp.isPlaying()) {
            mp.pause();
        }
    }

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

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

注册服务AndroidManifest.xml

最终能实现后台播放

你可能感兴趣的:(安卓基础学习,android)