Android---mediaplayer 创建和调用顺序

缥帙各舒散,前后互相逾。这篇文章主要讲述Android---mediaplayer 创建和调用顺序相关的知识,希望能为你提供帮助。
android mediaframework创建mediaplayer // java层///frameworks/base/media/java/android/media/MediaPlayer.java
public MediaPlayer() {
        ...
【Android---mediaplayer 创建和调用顺序】        native_setup(new WeakReference< MediaPlayer> (this));
        baseRegisterPlayer();
}
 
// native层///frameworks/base/media/jni/android_media_MediaPlayer.cpp
//将函数native_setup映射为android_media_MediaPlayer_native_setup
{"native_setup",               "(Ljava/lang/Object; )V",
(void*)android_media_MediaPlayer_native_setup},
android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
{
        sp< MediaPlayer> mp = new MediaPlayer();
        if (mp == NULL) {
                jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
                return;
        }
        sp< JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
mp-> setListener(listener);
        setMediaPlayer(env, thiz, mp);
}
 
Android mediaframework调用setDatasource // java层///frameworks/base/media/java/android/media/MediaPlayer.java
void setDataSource(String path, String[] keys, String[] values,
                List< HttpCookie> cookies)
                throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        ...
        if ("file".equals(scheme)) {
                path = uri.getPath();
        } else if (scheme != null) {
                // handle non-file sources
                nativeSetDataSource(MediaHTTPService.createHttpServiceBinderIfNecessary(path, cookies), path, keys, values);
                return;
        }
}
 
// 这两个函数是native层的代码,并且在库 ‘libmedia_jni.so‘中例如 :System.loadLibrary("media_jni");
private native  void nativeSetDataSource(IBinder httpServiceBinder, String path, String[] keys,
String[]  values)throws IOException, IllegalArgumentException,
SecurityException, IllegalStateException;
private native  void _setDataSource(FileDescriptor fd, long offset, long length)
                throws IOException, IllegalArgumentException, IllegalStateException;
 
// native层///frameworks/base/media/jni/android_media_MediaPlayer.cpp
//将函数nativeSetDataSource映射为android_media_MediaPlayer_setDataSourceAndHeaders
static const JNINativeMethod gMethods[] = {
        {
                "nativeSetDataSource",
                "(Landroid/os/IBinder; Ljava/lang/String; [Ljava/lang/String; "
                "[Ljava/lang/String; )V",
                (void *)android_media_MediaPlayer_setDataSourceAndHeaders
        }
        ...
}
 
static void  android_media_MediaPlayer_setDataSourceAndHeaders(
                JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path,
                jobjectArray keys, jobjectArray values) {
        sp< MediaPlayer> mp = getMediaPlayer(env, thiz);
        if (mp == NULL ) {
                jniThrowException(env, "java/lang/IllegalStateException", NULL);
                return;
        }
        ...
        status_t opStatus =
                mp-> setDataSource(
                                httpService,
                                pathStr,
                                headersVector.size() > 0? & headersVector : NULL);
        ...
}
 
/frameworks/av/media/libmedia/mediaplayer.cpp
status_t MediaPlayer::setDataSource(
                const sp< IMediaHTTPService> & httpService,
                const char *url, const KeyedVector< String8, String8> *headers)
{
        status_t err = BAD_VALUE;
if (url != NULL) {
        // 获取mediaserver系统服务进程中的MediaPlayerService的
                const sp< IMediaPlayerService> service(getMediaPlayerService());
                if (service != 0) {
                          // 创建一个MediaPlayer代理
                        sp< IMediaPlayer> player(service-> create(this, mAudiosessionId));
                        ...
                        err = attachNewPlayer(player);   // mPlayer = player;
                }
        }
        return err;
}

    推荐阅读