安卓端的音乐播放

需求:播放音乐,暂停音乐,停止音乐,退出

Activity版

    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.btn_play);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_exit = (Button) findViewById(R.id.btn_exit);
        btn_pause = (Button) findViewById(R.id.btn_pause);

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

    @Override
    public void onClick(View v) {
        if (btn_play == v) {
            play();
        } else if (btn_stop == v) {
            stopMusic();
        } else if (btn_exit == v) {
            exit();
        } else if (btn_pause == v) {
            pauseMesic();
        }
    }

    private MediaPlayer player;
    private void pauseMesic() {
        if (player!=null && player.isPlaying()) {
            player.pause();
        }
    }

    private void exit() {
        stopMusic();
        finish();
    }

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

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

这一种有一个大Bug,在点击返回键后音乐继续播放,但是重新打开软件时,点击其余的按钮都没有反应,这就是Activity与Service的区别,接着Service端

 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.btn_play);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_exit = (Button) findViewById(R.id.btn_exit);
        btn_pause = (Button) findViewById(R.id.btn_pause);

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

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,PlayMusic.class);
        if (btn_play == v) {
            intent.putExtra("action","play");
            startService(intent);
        } else if (btn_stop == v) {
            intent.putExtra("action","stop");
            startService(intent);
        } else if (btn_exit == v) {
            stopService(intent);
            finish();
        } else if (btn_pause == v) {
            intent.putExtra("action","pause");
            startService(intent);
        }
    }

需要新建一个Service用于上面几个按钮的实现

package shangguigu.java.playmusic;

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

public class PlayMusic extends Service {
    public PlayMusic() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    private MediaPlayer player;
    private void pauseMesic() {
        if (player!=null && player.isPlaying()) {
            player.pause();
        }
    }

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

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

    @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)) {
            pauseMesic();
        } else if ("stop".equals(action)) {
            stopMusic();
        }
        return super.onStartCommand(intent, flags, startId);
    }

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

调用service时需要使用onStartCommand方法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值