转:Android下的PVPlayer的实现

作者:jl

我们知道,MediaPlayerInterface接口Android框架中承上启下的关键接口,Android下面几个播放器都是冲这个接口派生过来的,前面在写flac的时候已经基本看了一些关于OGG player的相关东西,但是那个只是音频,还没有涉及到视频,下面简单的介绍一下其中最复杂的PVPlayer

class PVPlayer : public MediaPlayerInterface
{
public:
                        PVPlayer();
    virtual             ~PVPlayer();

    virtual status_t    initCheck();  //1
    virtual status_t    setDataSource(const char *url);//2
    virtual status_t    setDataSource(int fd, int64_t offset, int64_t length);//2
    virtual status_t    setVideoSurface(const sp<ISurface>& surface);//3
    virtual status_t    prepare();//4
    virtual status_t    prepareAsync();//5
    virtual status_t    start();//5
    virtual status_t    stop();//6
    virtual status_t    pause();//6
    virtual bool        isPlaying();
    virtual status_t    seekTo(int msec);
    virtual status_t    getCurrentPosition(int *msec);
    virtual status_t    getDuration(int *msec);
    virtual status_t    reset();
    virtual status_t    setLooping(int loop);
    virtual player_type playerType() { return PV_PLAYER; }

    // make available to PlayerDriver
    void        sendEvent(int msg, int ext1=0, int ext2=0) { MediaPlayerBase::sendEvent(msg, ext1, ext2); }

private:
    static void         do_nothing(status_t s, void *cookie, bool cancelled) { }
    static void         run_init(status_t s, void *cookie, bool cancelled);
    static void         run_set_video_surface(status_t s, void *cookie, bool cancelled);
    static void         run_set_audio_output(status_t s, void *cookie, bool cancelled);
    static void         run_prepare(status_t s, void *cookie, bool cancelled);

    PlayerDriver*               mPlayerDriver;
    char *                      mDataSourcePath;
    bool                        mIsDataSourceSet;
    sp<ISurface>                mSurface;
    int                         mSharedFd;
    status_t                    mInit;
    int                         mDuration;

#ifdef MAX_OPENCORE_INSTANCES
    static volatile int32_t     sNumInstances;
#endif
};

1、 virtual status_t    initCheck();  //这个函数会在我们创建播放器的时候调用,可以做一些基本的初始化工作,如下所示:

static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
        notify_callback_f notifyFunc)
{
    sp<MediaPlayerBase> p;
    switch (playerType) {
        case PV_PLAYER:
            LOGV(" create PVPlayer");
            p = new PVPlayer();
            break;
        case SONIVOX_PLAYER:
            LOGV(" create MidiFile");
            p = new MidiFile();
            break;
        case VORBIS_PLAYER:
            LOGV(" create VorbisPlayer");
            p = new VorbisPlayer();
            break;
    }
    if (p != NULL) {
        if (p->initCheck() == NO_ERROR) {
            p->setNotifyCallback(cookie, notifyFunc);
        } else {
            p.clear();
        }
    }
    if (p == NULL) {
        LOGE("Failed to create player object");
    }
    return p;
}

发现还有setNotifyCallback函数也会一起调用。

2、 两种设置源的方式 setDataSource,一个是打开一个文件重头开始,一个事从文件的中间开始。

status_t MediaPlayerService::Client::setDataSource(const char *url)
{
    LOGV("setDataSource(%s)", url);
    if (url == NULL)
        return UNKNOWN_ERROR;

    if (strncmp(url, "content://", 10) == 0) {
        // get a filedescriptor for the content Uri and
        // pass it to the setDataSource(fd) method

        String16 url16(url);
        int fd = android:penContentProviderFile(url16);//由content得到文件的句柄
        if (fd < 0)
        {
            LOGE("Couldn't open fd for %s", url);
            return UNKNOWN_ERROR;
        }
        setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
        close(fd);
        return mStatus;
    } else {
        player_type playerType = getPlayerType(url);
        LOGV("player type = %d", playerType);

        // create the right type of player
        sp<MediaPlayerBase> p = createPlayer(playerType);
        if (p == NULL) return NO_INIT;

        if (!p->hardwareOutput()) {
            mAudioOutput = new AudioOutput();
            static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
        }

        // now set data source
        LOGV(" setDataSource");
        mStatus = p->setDataSource(url);
        if (mStatus == NO_ERROR) mPlayer = p;
        return mStatus;
    }
}

连接:http://www.zixundao.com/thread-691-1-2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C_see

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值