们知道,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函数也会一起调用。