MediaPlayer+MediaPlayerController+MediaPlayer.OnBufferingUpdateListener实现音乐播放器

在Android开发中渐渐发现每一个类只负责自己的模块,类之间的耦合性很低。每一个类只负责自己的模块,但不同的类进行组合就可以创建一个功能强大的应用;

下面通过一个音乐播放器来体验下这个思想:
我们之前可能学过如何通过MediaPlayer来播放res/raw、assets中的原声文件、网络中的文件等等。在这里不做介绍,感兴趣的自己查资料吧!
但在以前我们更多的使用Button+MediaPlayer.start()/MediaPlayer.stop()/MediaPlayer.pause()等方法,来控制音乐的暂停和停止。其实Android为我们提供了MediaContraller来进行暂停、停止的请求。

1.MediaPlayerController的源码

public interface MediaPlayerControl {
   
        void    start();
        void    pause();
        int     getDuration();
        int     getCurrentPosition();
        void    seekTo(int pos);
        boolean isPlaying();
        int     getBufferPercentage();
        boolean canPause();
        boolean canSeekBackward();
        boolean canSeekForward();
    }

其实它就是一个接口,当我们通过MediaController来控制音乐的播放时,就会调用我们所实现它的方法。

2、MediaPlayer.OnBufferingUpdateListener

public interface OnBufferingUpdateListener
    {
        /**
         * Called to update status in buffering a media stream received through
         * progressive HTTP download. The received buffering percentage
         * indicates how much of the content has been buffered or played.
         * For example a buffering update of 80 percent when half the content
         * has already been played indicates that the next 30 percent of the
         * content to play has been buffered.
         *
         * @param mp      the MediaPlayer the update pertains to
         * @param percent the percentage (0-100) of the content
         *                that has been buffered or played thus far
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值