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

本文介绍了在Android开发中使用MediaPlayer、MediaController和MediaPlayer.OnBufferingUpdateListener实现音乐播放器的过程。通过一个Demo展示了如何结合这些组件,以及如何处理网络流媒体的缓冲监听。此外,还提到了MediaController在布局和Activity中的使用技巧,并提到可以使用相同的原理控制VideoView播放视频。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值