码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2020-06-16 12:51:34      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:file   player   scheme   创建   return   listen   weakref   vat   desc   

Android mediaframework创建mediaplayer

// java

///frameworks/base/media/java/android/media/MediaPlayer.java

public 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;

}

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

标签:file   player   scheme   创建   return   listen   weakref   vat   desc   

原文地址:https://www.cnblogs.com/grandblogs/p/13140089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!