1、music.apk to Java mediaplayer to libmedia mediaplayer (JNI)
Start is a Java Space Code. In frameworks/base/Media/Java/Android/Media/mediaplayer. Java at the framework layer,
The JNI and Android binder mechanisms are used to call the implementation to the C ++ space, and the mediaplayer library is packaged up for application development and use.
/** Description: * starts or resumes playback. if playback had previusly been paused, * playback will continue from where it was paused. if playback had * been stopped, or never started before, playback will start at the * beginning. ** @ throws illegalstateexception if it is called in an invalid state */Public void start () throws illegalstateexception {stayawake (true); _ start ();}
Private native void _ start () throws illegalstateexception;
_ Start is a local method, which calls Android implementation through JNI. The implementation code is located:
Frameworks/base/Media/JNI/android_media_mediaplayer.cpp
The jninativemethod gmethods [] array defines the correspondence between JVM and C ++ functions, including the correspondence between function names and passing parameters,
For example, {"_ start", "() V", (void *) android_media_mediaplayer_start },
Where:
"_ Start" is the Java space function name.
"() V" is a Java space parameter and function type. It is a parameter in brackets. It is used. Multiple parameters are separated. The brackets are followed by the function type. V indicates void, I means int, etc.
"(Void *) android_media_mediaplayer_start" corresponds to the C ++ spatial function type and function name.
static voidandroid_media_MediaPlayer_start(JNIEnv *env, jobject thiz){ LOGV("start"); sp<MediaPlayer> mp = getMediaPlayer(env, thiz); if (mp == NULL ) { jniThrowException(env, "java/lang/IllegalStateException", NULL); return; } process_media_player_call( env, thiz, mp->start(), NULL, NULL );}
Jnienv * env local space resource. jobject thiz is a Java space resource. getmediaplayer (ENV, thiz) first obtains the corresponding local resource from the Java space resource, and the return type is mediaplayer pointer; process_media_player_call call the mediaplayer class method start. So far, the call relationship from the start of the Java space to the start in the local implementation has been completed, and how to implement the start is the service thing.
The call process from the music.apk method start, pause/resume, stop, to the C ++ method to start, pause/resume, stop is the same