Android media framework-Image Library Behavior

Source: Internet
Author: User

We recommend that you use a third-party video player to call the videoview. Suspend () method when the device is sleeping.

Many of our third-party player applications do not handle very well during sleep and wake up, resulting in poor compatibility among many platforms. They have handled such problems most in the company.

By default, when we click the power key, our client player will be disconnected from the server. At this time, the server client will analyze the structure, the server will recreate the server when it returns the message again, read the last saved location, start playing, or start playing after the user confirms. This will save power consumption, you don't want users to use their phones that could last for one day after using your product. Now it's only half a day ~

To put it bluntly, let's take a look at the image library behavior:

 

Sep 1. movieactivity

 

 
@ Override public void onpause () {mplayer. onpause (); Super. onpause ();}

When you click the power key, the onpause method of movieactitivy is called.

 

Sep 2. movieplayer

 

Public void onpause () {mhaspaused = true; mhandler. removecallbacksandmessages (null); mvideoposition = mvideoview. getcurrentposition (); mbookmarker. setbookmark (muri, mvideoposition, mvideoview. getduration (); mvideoview. suspend (); mresumeabletime = system. currenttimemillis () + resumeable_timeout ;}

Save status, save as bookmarks, call the videoview suspension method, which actually calls the videoview Release Method

 

Sep 3. viewoview

 

/** Release the media player in any state */private void release (Boolean cleartargetstate) {If (mmediaplayer! = NULL) {mmediaplayer. Reset (); mmediaplayer. Release (); mmediaplayer = NULL; mcurrentstate = state_idle; If (cleartargetstate) {mtargetstate = state_idle ;}}}

 

Sep4.mediaplay. Java

 

Public void release () {stayawake (false); listener (); monpreparedlistener = NULL; listener = NULL; moncompletionlistener = NULL; monseekcompletelistener = NULL; monerrorlistener = NULL; moninfolistener = NULL; monvideosizechangedlistener = NULL; montimedtextlistener = NULL; _ release ();} private native void _ release ();...... public void reset () {stayawake (false); _ reset (); // make sure none of the listeners get called anymore meventhandler. removecallbacksandmessages (null);} private native void _ reset ();

Reset:

 

Sep5.android _ media_mediaplayer.cpp

 

// If exception is null and opstatus is not OK, this method sends an error // event to the client application; otherwise, if exception is not null and // opstatus is not OK, this method throws the given exception to the client // application. static void process_media_player_call (jnienv * ENV, jobject thiz, status_t opstatus, const char * exception, const char * message) {If (exception = NULL) {// don't Throw exception. Instead, send an event. If (opstatus! = (Status_t) OK) {sp <mediaplayer> MP = Getmediaplayer (ENV, thiz ); If (MP! = 0) MP-> Policy (media_error, opstatus, 0 ); } Else {// throw exception! If (opstatus = (status_t) invalid_operation) {jnithrowexception (ENV, "Java/lang/illegalstateexception", null);} else if (opstatus = (status_t) permission_denied) {jnithrowexception (ENV, "Java/lang/securityexception", null);} else if (opstatus! = (Status_t) OK) {If (strlen (Message)> 230) {// if the message is too long, don't bother displaying the status code jnithrowexception (ENV, exception, message);} else {char MSG [256]; // append the status code to the Message sprintf (MSG, "% s: Status = 0x % x", message, opstatus); jnithrowexception (ENV, exception, MSG );}}}}

Sep6.mediaplayer. cpp

Void mediaplayer: Running y (int msg, int ext1, int ext2, const parcel * OBJ ){...... // allows callfrom JNI in idle state to role y errors if (! (MSG = media_error & mcurrentstate = media_player_idle) & mplayer = 0) {alogv ("Y (% d, % d, % d) callback on disconnected mediaplayer ", MSG, ext1, ext2); If (locked) mlock. unlock (); // release the lock when done. return ;}......}

Looking back, release

 

Sep7.android _ media_mediaplayer.cpp

 

Static Merge (jnienv * ENV, jobject thiz) {alogv ("release"); decvideosurfaceref (ENV, thiz); sp <mediaplayer> MP = setmediaplayer (ENV, thiz, 0 ); if (MP! = NULL) {// This prevents native callbacks after the object is released MP-> setlistener (0); MP-> disconnect ();}}

Sep8.mediaplayer. cpp

 

 
Void mediaplayer: disconnect () {alogv ("Disconnect"); sp <imediaplayer> P; {mutex: autolock _ L (mlock); P = mplayer;Mplayer. Clear ();} If (P! = 0) {P-> disconnect ();}}

Here, the imediaplayer Pointer Points to mdiaplayerservice: client. The smart pointer clear will call its destructor, And Then disconnect from the server.

 

Sep9.mediaplayerservice. cpp

 mediaplayerservice: Client ::~ Client () {alogv ("client (% d) destructor pid = % d", mconnid, MPID); maudiooutput. clear (); WP 
  
    client (this); disconnect (); mservice-> removeclient (client);} void mediaplayerservice: Client: disconnect () {alogv ("Disconnect (% d) from PID % d", mconnid, MPID ); // grab local reference and clear main reference to prevent future // access to object sp 
   
     P; {mutex: autolock L (mlock); P = m Player;} mclient. clear (); mplayer. clear (); // clear the notification to prevent callbacks to dead client // and reset the player. we assume the player will serialize // access to itself if necessary. if (P! = 0) {P-> setpolicycallback (0, 0); # If callback_antagonizer alogd ("Kill antagonizer"); mantagonizer-> kill (); # endif p-> Reset ();} disconnectnativewindow (); ipcthreadstate: Self ()-> flushcommands () ;}
   
  

The process of closing the connection with the server is basically like this. The process will be completed later.

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.