Android4.1APi MediaPlayer

来源:互联网 发布:大数据世界txt免费下载 编辑:程序博客网 时间:2024/06/08 19:55

Android4.0新特性

public class

MediaPlayer

extends Object

1.      关于怎么去使用MediaPlayer,你可以去读MediaPlayback这个类来获取更多的的信息。

2.       状态图:视频音频的文件和流的Playback 控制被作为一个状态机器管理着,下面的图片展示了一个生命周期和一个Mediaplayer对象被playback控制器操作期间的各种状态。椭圆形的圈呈现了一个MediaPlayer可能属于的状态。弧线将会呈现playback控制器的操作使对象的状态得到转换。有两个弧形,一个是在单向同步方法 一个是双向异步方法的调用。

 

从上述的状态图可以看出,一个mediaPlayer对象是根据以下的状态:

1.       当一个Mediaplayer对象使用new或者在reset()方法之后产生时,它是进入到idle的状态,在调用release()方法之后,它会处于结束的状态。在这两者之间的就是它的生命周期。

2.       这里有个微妙但有十分重要的不同点在于一个new出来和在调用了reset()方法之后产生的MediaPlayer对象。相同点是:在idle状态下调用以下的方法时候都会可能出现错误 getCurrentPosition()getDuration()getVideoHeight()getVideoWidth(),setAudioStreamType(int)setLooping(boolean)setVolume(float,float)pause()start()stop()seekTo(int)prepare() orprepareAsync() 。如果任何一个上述的方法在MediaPlayer被构造之后被调用,调用者将不能通过player引擎调用OnErrorListener onEorre()这个回调的方法,但是这个方法在reset()之后可以被调用,并把对象转换去一个错误状态。

3.       一旦一个MediaPlayer对象不在被使用,你可以马上调用release()方法,这样可以使Media引擎及时地释放资源。内存可能包含了一些单一资源列如:使硬件加速的组件和调用release()失败所引起的MediaPlay对象实例。一旦MediaPlayer对象处于end状态,它可以不再被使用和不再使它返回到其他任何的状态了。

4.       更深入的是,一些已经经过重载的create方法并不在处于idel状态的Mediaplayer对象使用new关键字去实例化是在idle状态中的。实质上,这些对象在调用creat方法后已经进入了Prepared状体了。

5.       通常来说,一些playback控制操作可能因为一系列的原因,列如:一个不支持的格式,损坏的,解码过高的,加载失败的等等致使它操作失败。因此,错误报告和发现是一个非常重要的一环在这些生命周期中。有时候,由于过程的错误,调用一个在初始化状态的playback控制器操作也可能发生一些错误。对于这些错误,如果已经通过setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注册了一个onErronrListener的话.player引擎提醒使用者去调用onErrorListener.onError()方法。

1)一旦错误发生,note是非常重要的,Meidaplayer对象会进入Error状态,即使一个错误监听器没有在程序中注册。

2)为了重使用一个在错误状态和被错误覆盖的Mediaplayer这个对象,可以调用reset()方法去保存这个对象在idle状态中。

3)注册一个错误监听器去负责监听来自Mediaplayer引擎的错误提醒是一个很好的行为。

6.       在调用prepare().prepareAsync().或者任何一个重载setDataSource()方法在初始化状态下,非法状态异常将会被抛出。

7.       调用setDataSource()方法可以使一个Mediaplayer对象从Idle到初始化状态的转化。

8.       一个MediaPlayer播放器在playback加入之前必须首先进入prepaered状态的。到达prepeared状态的有两个方法(同步和异步),或者调用这个prepare(synchronous)去转化对象进入Prepared状态,或者去调用preparedAsync(异步)去转化,后者可以在播放引擎在直到准备工作完成钱工作。当准备工作完成或者prepared()被调用,Mediaplayer引擎将会提示用户去调用,onPreared.Listener接口中的oNpreareed()的回调方法。如果这个监听器之前就被注册了。注册这个接口是非常重要的,以为这个Preeared状态是一个非常短暂的状态,监听器能有效的调用其他方法当这个Mediaplayer对在这个状态中没有定义的话。一个非法状态异常会被抛出当在其他任何一个状态中调用preared()或者prearedAsync().当处于Perared这个状态中的时候,音频视频的声音,屏幕适应器在一系列的方法中。

9.       必须去调用start()才能是Meidaplaer开始。在成功调用开始方法之后,Mediaplayer会进入播放状态,可以调用isplaying来判断这个对象是否处于播放状态。当出于播放状态的时候,播放引擎会告诉使用者器要提供一个OnBufferingUpdate()的监听器,如果这个监听器之前就被注册了的话。这个回调方法可以使程序保持缓冲在视频和音频播放的时候。调用start()的方法在播放器已经处于播放状态的时候是没有用的。

10.   Playback可以被暂停和停止,当前的Playback位置可以被调整。Playback可以通过paused()方法暂停,当调用这个方法的时候,对象会进入暂停状态。提醒这个从播放状态进入暂停状态和异步方法在播放引擎中。它可能需要一些时间在状态更新的时候,也会话费数秒在流内存中。调用start()方法去恢复一个已经暂停的Mediaplayer和恢复playback的暂停时位置。在一个已经暂停的Mediaplayer中调用pause().

11.   调用stop()会停止一个playback和影响一个处于播放,准备,暂停或者完全播放完等状态去进去一个停止状态。一旦处于停止状态,playback不能再播放直到通过调用prepared()或者perparedAsync()去设置Mediaplayer再去进入Prepared状态。在Mediaplayer已经处于停止状态中的时候再调用stop方式会失效的.

12.   playback可以被调整当调用了seekto().,即使是异步去调用seekTo(int)是正确的方式。实际的跳过操作可能会话费一些是时间去完成,尤其是对一些视频或音频流的时候。当实际跳过操作完成了,player引擎会告诉使用者去提供一个已经注册了的OnSeekComplete.onSeekComplete()接口方法,请记住seekTo(int)的方法也可以在其他的状态下被调用,例如在Prepared.PausePlaybackCompleted的状态下。更深入的是,当前playback的实际位置可以被检索,通过调用一个getCurentPosition()的方法,这个方法对于一些音乐播放器来说非常有用。

13.   当一个playback被播放到尽头的时候。如果looping模式被setLooping(boolean)方法设置为true.的时候,Mediaplayerh会停留在播放状态。如果looping模式被设置为false,player引擎会告诉使用者提供一个回调的方法OnCompletion.OnCompletion(),如果这个接口已经被实现了。这个回调方法标志着这个Mediaplayer已经进入到PlaybackComPletion状态。当进入到这个状态的时候再调用start()方法可以使playbcak重头开始。

API中PLayBack说明:

1.Note: If you're passing a URL to stream anonline media file, the file must be capable of progressive download.

Caution: You must either catch or pass IllegalArgumentException and IOException when usingsetDataSource(), becausethe file you are referencing might not exist.

3.       Asynchronous Preparation

4.       Managing State

5.       Releasing the MediaPlayer

6.       Using a Service with MediaPlaye

7.      Running asynchronously

    .          For instance, when using a MediaPlayer from your mainthread, you should call prepareAsync() rather thanprepare(), and implement aMediaPlayer.OnPreparedListener in order to be notified when the preparation iscomplete and you can start playing. For example:

8.       Handling asynchronous errors 

          public class MyServiceextends Service implements MediaPlayer.OnPreparedListener

 

9.       cpu wake and wifi wake   使CPU wifi在屏幕黑掉的时候保持工作,记得释放资源当不在使用的时候。

10.  Running as aforeground service 很多时候后台的service被打断了或者重新开始,用户都是很难察觉的或者不知道什么时候被调用。使用前台服务会获得更高的使用级别,不至于被设备kill.使用ActionBar让用户看到可以和服务联系。为了使你的服务成为一个前台进程,你必须要实现一个Notifycation在状态栏中和使用服务中的startFourground()方法。当不再使用一个前台服务的时候要stopForeground(true);释放它。

String songName;

// assign the songname to songName

PendingIntent pi =PendingIntent.getActivity(getApplicationContext(), 0,

                newIntent(getApplicationContext(), MainActivity.class),

               PendingIntent.FLAG_UPDATE_CURRENT);

Notificationnotification = new Notification();

notification.tickerText= text;

notification.icon =R.drawable.play0;

notification.flags|= Notification.FLAG_ONGOING_EVENT;

notification.setLatestEventInfo(getApplicationContext(),"MusicPlayerSample",

                "Playing: " +songName, pi);

startForeground(NOTIFICATION_ID,notification);

 

 

11.    

Using wake locks

When designing applications that play media inthe background, the device may go to sleep while your service is running.Because the Android system tries to conserve battery while the device issleeping, the system tries to shut off any of the phone's features that are notnecessary, including the CPU and the WiFi hardware. However, if your service isplaying or streaming music, you want to prevent the system from interferingwith your playback.

In order to ensure that your service continuesto run under those conditions, you have to use "wake locks." A wakelock is a way to signal to the system that your application is using somefeature that should stay available even if the phone is idle.

Notice: You should always use wake locks sparinglyand hold them only for as long as truly necessary, because they significantlyreduce the battery life of the device.

To ensure that the CPU continues running whileyour MediaPlayer is playing, call the setWakeMode() method when initializing your MediaPlayer. Once you do, the MediaPlayer holds the specified lock while playingand releases the lock when paused or stopped:

mMediaPlayer = new MediaPlayer();
// ... other initialization here ...
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

However, the wake lock acquired in thisexample guarantees only that the CPU remains awake. If you are streaming mediaover the network and you are using Wi-Fi, you probably want to hold a WifiLock as well, which you must acquire andrelease manually. So, when you start preparing the MediaPlayer with the remote URL, you should createand acquire the Wi-Fi lock. For example:

WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
    .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
 
wifiLock.acquire();

When you pause or stop your media, or when youno longer need the network, you should release the lock:

wifiLock.release();
原创粉丝点击