Libmedia MediaPlayer (JNI)

The Start is a Java code space, in the framework layer frameworks/base/media/Java/android/media/mediaPlayer. In Java,

Through JNI and android binder mechanisms, the implementation is called into C++ space. Package up into the MediaPlayer library for application development.

[csharp] view plain copy

  1. / * * description:
  2.  * Starts or resumes playback. If playback had previously been paused, 
  3.  * playback will continue from where it was paused. If playback had 
  4.  * been stopped, or never started before, playback will start at the 
  5.  * beginning. 
  6.  * 
  7.  * @throws IllegalStateException if it is called in an invalid state 
  8. * /
  9. public  void start() throws IllegalStateException {  
  10.     stayAwake(true);  
  11.     _start();  
  12. }  

\

private native void _start() throws IllegalStateException;

_start is a local method that calls the Android implementation via JNI. The implementation code is located at:

frameworks/base/media/jni/android_media_MediaPlayer.cpp

JNINativeMethod gMethods[] array defines the mapping between JVM and C++ functions, including function names and transfer parameters.

Such as {” _start “, “V” (), (void *) android_media_MediaPlayer_start},

Among them:

“_start” is the Java space function name

“()V” is the Java spatial parameter and function type. Separate multiple arguments, parentheses followed by function types, V for void, I for int, and so on

(void *)android_media_MediaPlayer_start is the corresponding C++ spatial function type and function name

[csharp] view plain copy

  1. static void  
  2. android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)  
  3. {  
  4.     LOGV(“start”);  
  5.     sp<MediaPlayer> mp = getMediaPlayer(env, thiz);  
  6.     if (mp == NULL ) {  
  7.         jniThrowException(env, “java/lang/IllegalStateException”, NULL);  
  8.         return;  
  9.     }  
  10.     process_media_player_call( env, thiz, mp->start(), NULL, NULL );  
  11. }  

\

JNIEnv *env local space resource, jobject thiz is Java space resource; GetMediaPlayer (env, thiz) getMediaPlayer(env, thiz) getMediaPlayer(env, thiz) Process_media_player_call calls the method start of the MediaPlayer class. At this point, we have completed the call relationship from start in Java space to start in the local implementation. After that, it is up to service to implement the start.

\

From music. apk method start, pause/resume, stop, C++ implementation of start, pause/resume, stop call process is the same

\