Android audio process 1 (JNI)

Source: Internet
Author: User

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



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.