video/audio playback:setDataSource

本文详细介绍了在Android系统中实现多媒体播放器的过程,包括创建播放器、设置数据源、准备和开始播放等关键步骤。特别关注了不同类型的URL处理方式,如content://和http://,以及如何根据文件类型选择合适的播放器。
摘要由CSDN通过智能技术生成

实现video/audio playerback,需要经过如下步骤:

MediaPlayer mp = new MediaPlayer();   

mp.setDataSource(String URL/FILE_PATH);   

mp.prepare();   

mp.start();

 

首先是setDataSource创建player,如果url是以content://形式的转换为FD,并提取出mAudioTrack/mVideoTrack保存起来,如果url是以http://之类的在线播放多媒体信息,需去下载SDP文件,找到url存储起来

1.framework/base/media/libmedia/ MediaPlayer.cpp

status_tMediaPlayer::setDataSource(

       const char *url, const KeyedVector<String8, String8> *headers) {

     (1)  const sp<IMediaPlayerService>& service(getMediaPlayerService()); ……首先获得mediaplayerService的服务

    if (service != 0) {

    2    sp<IMediaPlayer> player(service->create(getpid(), this, fd, offset, length, mAudioSessionId));

    3 err = setDataSource(player);

    }

    return err;

}

2. framework/base/media/libmediaplayerservice/ MediaPlayerService.cpp

sp<IMediaPlayer> MediaPlayerService::create(

        pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,

        const KeyedVector<String8, String8> *headers, int audioSessionId)

{

….

    sp<Client> c = new Client(this, pid, connId, client, audioSessionId); …. 新建个mediaplayerservice client 的实例,即整个播放过程由这个实例控制

………

if (NO_ERROR != c->setDataSource(url, headers))…..create playersetDataSource

    {

        c.clear();

        return c;

    }

   

}

 

status_t MediaPlayerService::Client::setDataSource(

        const char *url, const KeyedVector<String8, String8> *headers)

{….

如果是url content://开头要转换为file descriptor

    if (strncmp(url, "content://", 10) == 0) {…

        int fd = android::openContentProviderFile(url16);

……….

        setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus

        close(fd);

        return mStatus;

    } else {

        player_type playerType = getPlayerType(url);…. createplayer前要判断是哪种类型

        LOGV("player type = %d", playerType);

 

        // create the right type of player

        sp<MediaPlayerBase> p = createPlayer(playerType);

        mStatus = p->setDataSource(url, headers);

…       

return mStatus;

    }

}

 

player_type getPlayerType(const char* url) …………….根据url的后缀名判断属于哪种playerType,默认是stagefright

{

    if (TestPlayerStub::canBeUsed(url)) {

        return TEST_PLAYER;

    }

 

    // use MidiFile for MIDI extensions

    int lenURL = strlen(url);

    for (int i = 0; i < NELEM(FILE_EXTS); ++i) {

        int len = strlen(FILE_EXTS[i].extension);

        int start = lenURL - len;

        if (start > 0) {

            if (!strncasecmp(url + start,FILE_EXTS[i].extension, len)) {

                return FILE_EXTS[i].playertype;

            }

        }

    }

……………….

    return getDefaultPlayerType();

}

 

有如下类型的playerType

 

typedef struct {

    const char *extension;

    const player_type playertype;

} extmap;

extmap FILE_EXTS [] =  {

        {".mid", SONIVOX_PLAYER},

        {".midi", SONIVOX_PLAYER},

        {".smf", SONIVOX_PLAYER},

        {".xmf", SONIVOX_PLAYER},

        {".mxmf", SONIVOX_PLAYER},

        {".imy", SONIVOX_PLAYER},

        {".rtttl", SONIVOX_PLAYER},

        {".rtx", SONIVOX_PLAYER},

        {".ota", SONIVOX_PLAYER},

        {".mnv", SOLS_PLAYER},

        {".mnvtmp", SOLS_PLAYER},

#ifndef NO_OPENCORE

        {".wma", PV_PLAYER},

        {".wmv", PV_PLAYER},

        {".asf", PV_PLAYER},

#endif

};

 

 sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)

{…….  if (p == NULL) {

        p = android::createPlayer(playerType, this, notify);

    }

    return p;

}

 

static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,

        notify_callback_f notifyFunc)

{

    sp<MediaPlayerBase> p;

    switch (playerType) {

#ifndef NO_OPENCORE

        case PV_PLAYER:

            LOGV(" create PVPlayer");

            p = new PVPlayer();

            break;

#endif

        case SONIVOX_PLAYER:

            LOGV(" create MidiFile");

            p = new MidiFile();

            break;

        case STAGEFRIGHT_PLAYER:

            LOGV(" create StagefrightPlayer");

            p = new StagefrightPlayer;   

            break;

        case TEST_PLAYER:

            LOGV("Create Test Player stub");

            p = new TestPlayerStub();

            break;

        case SOLS_PLAYER:

            LOGV(" create SolsStagefrightPlayer");

            p = new SolsStagefrightPlayer;

            break;

    }….

    return p;

}

 

3, framework/base/media/libmediaplayerservice/StagefrightPlayer.cpp

创建stagefrightplayer实例也new了个AwesomePlayer(mPlayer)

StagefrightPlayer::StagefrightPlayer()

    : mPlayer(new AwesomePlayer) {

    LOGV("StagefrightPlayer");

 

    mPlayer->setListener(this);

}

4, framework/base/media/libStagefright/ AwesomePlayer.cpp

AwesomePlayer setDataSource 根据传过来的是 url(http://..),还是fd进行不同的处理

url:记录下url  http request header , mediaextract::finishSetDataSource_l中进行。

status_t AwesomePlayer::setDataSource_l(

        const char *uri, const KeyedVector<String8, String8> *headers) {

    LOGV("setDataSource '%s'", uri);

    // Check for sdp

    status_t err;

    if (headers) {

        mUriHeaders = *headers;

    }…….

    if (extension.length() == 4 && !strncasecmp(extension.string(), ".sdp", 4)) {

        //found sdp

        if (!strncasecmp("http://", tmpString8Uri.string(), 7)) {

            LOGV("found SDP file over http");

            String8 newUri;

            err = getUriFromSDP(uri, &newUri);…………..Download SDP, parse and return found URI                    SDP:Service Data Point业务数据点

            if (err == OK) {

                return setDataSource_l(newUri.string(), headers);

            } else {

..

            }

        }

    }

    reset_l();

    mUri = uri;

    return OK;

}

FD形式:根据FD生成filesource,后找到匹配的mediaextractor--->mVideoTrack/mAudioTrack

status_t AwesomePlayer::setDataSource(

       int fd, int64_t offset, int64_t length) {

    Mutex::Autolock autoLock(mLock);

    reset_l();

    sp<DataSource> dataSource = new FileSource(fd, offset, length);

…..

    return setDataSource_l(dataSource);

}

status_t AwesomePlayer::setDataSource_l(

       const sp<DataSource> &dataSource) {

    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);…….创建对应的extractor

…..

    return setDataSource_l(extractor);

}

status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) {

        if (!haveVideo && !strncasecmp(mime, "video/", 6)) {

            setVideoSource(extractor->getTrack(i)); ………>mVideoTrack

            haveVideo = true;

        } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {

            setAudioSource(extractor->getTrack(i));……….>mAudioTrack

            haveAudio = true;

    return OK;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值