Mediaplayer framework Overview (1)

Source: Internet
Author: User
1. Media Player Overview

Android mediaplayer includes the playing functions of audio and video. On the android interface, both the music and video applications call mediaplayer.
Mediaplayer is implemented at the underlying layer based on the opencore (packetvideo) Library. To build a mediaplayer program, the upper layer also contains inter-process communication and other content, the basis for inter-process communication is the Binder Mechanism in the basic Android library.

Taking open-source android as an example, the mediaplayer code is mainly in the following directory:
Java program path:
Packages/apps/music/src/COM/Android/music/

Java class path:
Frameworks/base/Media/Java/Android/Media/mediaplayer. Java

Java local call (JNI ):

Frameworks/base/Media/JNI/android_media_mediaplayer.cpp is compiled to libmedia_jni.so.

The main header file is in the following directory:

Frameworks/base/include/Media/

The multimedia underlying library is in the following directory:

Frameworks/base/Media/libmedia/
This part of content is compiled into the library libmedia. So.

Multimedia Service Section:
Frameworks/base/Media/libmediaplayerservice/
The files are mediaplayerservice. h and mediaplayerservice. cpp.
This part of content is compiled into the library libmediaplayerservice. So.

Opencore-based multimedia player
External/opencore/
This part of content is compiled into library libopencoreplayer. So.

From the perspective of program scale, libopencoreplayer. So is the main implementation part, while other libraries are basically encapsulated on it and the mechanism for establishing inter-process communication.
2. Levels of media players

1. Java program section

The mediaplaybackservice. Java file in the packages/apps/music/src/COM/Android/music/directory contains the call to the mediaplayer. Include a reference to the package in mediaplaybackservice. Java: Import Android. Media. mediaplayer;

In the mediaplaybackservice class, the multiplayer class is defined:

private class MultiPlayer {   private MediaPlayer mMediaPlayer = new MediaPlayer();}

2. header files

The header file of mediaplayer is in the frameworks/base/include/Media/directory, which corresponds to the directory of the libmedia. So Library source file frameworks/base/Media/libmedia. The main header files include the following:
Imediaplayerclient. h is used to describe the interface of a mediaplayer client.
Mediaplayer. h defines a mediaplayer class.
Imediaplayer. h implements the mediaplayer function interface
Imediaplayerservice. h describes a mediaplayer service.

In these header files, mediaplayer. H provides interfaces for the upper layer, while several other header files provide some interface classes (that is, classes containing pure virtual functions ), these interface classes must be inherited by implementation classes before they can be used.

2. Java local call of mediaplayer

The Java local call of mediaplayer is implemented in the file frameworks/base/Media/JNI/android_media_mediaplayer.cpp.
Android_media_mediaplayer.cpp defines a jninativemethod (Java local call method) type array gmethods, as shown below:

static JNINativeMethod gMethods[] = {{"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaPlayer_setDataSource},{"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD},    {"prepare", "()V", (void *)android_media_MediaPlayer_prepare},    {"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},    {"_start", "()V", (void *)android_media_MediaPlayer_start},    {"_stop", "()V", (void *)android_media_MediaPlayer_stop},    {"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth},    {"getVideoHeight", "()I", (void *)android_media_MediaPlayer_getVideoHeight},    {"seekTo", "(I)V", (void *)android_media_MediaPlayer_seekTo},    {"_pause", "()V", (void *)android_media_MediaPlayer_pause},    {"isPlaying", "()Z", (void *)android_media_MediaPlayer_isPlaying},    {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer_getCurrentPosition},    {"getDuration", "()I", (void *)android_media_MediaPlayer_getDuration},    {"_release", "()V", (void *)android_media_MediaPlayer_release},    {"_reset", "()V", (void *)android_media_MediaPlayer_reset},    {"setAudioStreamType", "(I)V", (void *)android_media_MediaPlayer_setAudioStreamType},    {"setLooping", "(Z)V", (void *)android_media_MediaPlayer_setLooping},    {"setVolume", "(FF)V", (void *)android_media_MediaPlayer_setVolume},    {"getFrameAt", "(I)Landroid/graphics/Bitmap;", (void *)android_media_MediaPlayer_getFrameAt},    {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup},    {"native_finalize", "()V", (void *)android_media_MediaPlayer_native_finalize},}

The first member of jninativemethod is a string that represents the name of the Java local call method. This name is called in the Java program. The second member is also a string, indicates the parameters and return values of the local call method in Java. The Third Member is the C language function corresponding to the local call method in Java.

The android_media_mediaplayer_start function is implemented as follows:

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 );}

In the call of android_media_mediaplayer_start, A mediaplayer pointer is obtained, and the actual function is implemented by calling it.

Register_android_media_mediaplayer is used to register gmethods as a class "android/Media/mediaplayer". Its implementation is as follows:

// This function only registers the native methodsstatic int register_android_media_MediaPlayer(JNIEnv *env){    return AndroidRuntime::registerNativeMethods(env,                "android/media/MediaPlayer", gMethods, NELEM(gMethods));}

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.