在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