android5.0 音乐播放,加载extractor

android5.0和以前的版本有变动,音乐播放不在走awesomeplay,audiotrack了;现在走nuplayerdriver,nuplayer,nuplayerrender。

音乐播放:

1。mediaplay

[cpp]  view plain copy
  1. status_t MediaPlayer::prepare()  
  2. {  
  3.       
  4.     status_t ret = prepareAsync_l();      
  5. }  
  6.   
  7. status_t MediaPlayer::prepareAsync_l()  
  8. {  
  9.   
  10.         return mPlayer->prepareAsync();  
  11. }  
mplayer就是nuplayerdriver了,不在是awesome了。这是5.0的改动。

2.NuplayerDriver

[cpp]  view plain copy
  1. status_t NuPlayerDriver::prepareAsync() {  
  2.       
  3.     switch (mState) {  
  4.         case STATE_UNPREPARED:  
  5.             mState = STATE_PREPARING;  
  6.             mIsAsyncPrepare = true;  
  7.             mPlayer->prepareAsync();  
  8.             return OK;  
  9. }  

这里是准备阶段,刚开始准备,所示state是STATE_UNPREPARED。

可以看到mPlayer的定义:    sp<NuPlayer> mPlayer;,即上面调用了Nuplayer。

3.Nuplayer

[cpp]  view plain copy
  1. void NuPlayer::prepareAsync() {  
  2.     (new AMessage(kWhatPrepare, id()))->post();  
  3. }  
这是发送消息:kWhatPrepare。

在这个文件里有消息接受处理函数。

[cpp]  view plain copy
  1. void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {  
  2.     switch (msg->what()) {  
  3.         case kWhatPrepare:  
  4.         {  
  5.             mSource->prepareAsync();  
  6.             break;  
  7.         }  
  8. }  
这里调用mSource的prepareAsync函数,但是mSource是谁呢?

还是在这个文件里面,我们来看一下代码:

[cpp]  view plain copy
  1. void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {  
  2.     sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());  
  3.   
  4.     sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());  
  5.   
  6.     sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID);  
  7.   
  8.     status_t err = source->setDataSource(fd, offset, length);  
  9.   
  10.     if (err != OK) {  
  11.         ALOGE("Failed to set data source!");  
  12.         source = NULL;  
  13.     }  
  14.   
  15.     msg->setObject("source", source);  
  16.     msg->post();  
  17. }  

GenericSource ,也就是调用到了 GenericSource 的prepareAsync函数。

4.GenericSource

[cpp]  view plain copy
  1. void NuPlayer::GenericSource::prepareAsync() {  
  2.     if (mLooper == NULL) {  
  3.         mLooper = new ALooper;  
  4.         mLooper->setName("generic");  
  5.         mLooper->start();  
  6.   
  7.         mLooper->registerHandler(this);  
  8.     }  
  9.   
  10.     sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());  
  11.     msg->post();  
  12. }  

这里还是消息机制,发送一个kWhatPrepareAsync消息。

那么发送消息了,必然有接受消息的,在这个文件中,如下函数:

[cpp]  view plain copy
  1. void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {  
  2.     switch (msg->what()) {  
  3.       case kWhatPrepareAsync:  
  4.       {  
  5.           onPrepareAsync();  
  6.           break;  
  7.       }  
  8. }  
调用了onPrepareAsync函数。我也觉得很烦,为什么不知接调用,要这么麻烦的不断采用消息机制,理解了的TX帮忙解释下吧。

[cpp]  view plain copy
  1. void NuPlayer::GenericSource::onPrepareAsync() {  
  2. 。。。。。。。。  
  3.          else {  
  4.             // set to false first, if the extractor  
  5.             // comes back as secure, set it to true then.  
  6.             mIsWidevine = false;  
  7.   
  8.             mDataSource = new FileSource(mFd, mOffset, mLength);  
  9.          }  
  10. 。。。。  
  11. // init extrator from data source  
  12.     err = initFromDataSource();  
  13. }  
这里,initFromDataSource就是去extractor了。

[cpp]  view plain copy
  1. status_t NuPlayer::GenericSource::initFromDataSource() {  
  2.     sp<MediaExtractor> extractor;  
  3. 。。。。。  
  4. else {    
  5.         extractor = MediaExtractor::Create(mDataSource,  
  6.                 mSniffedMIME.empty() ? NULL:   
  7.     }  
  8. 。。。  
  9. }  

5.MediaExtractor

进入到MediaExtractor.cpp

[cpp]  view plain copy
  1. sp<MediaExtractor> MediaExtractor::Create(  
  2.         const sp<DataSource> &source, const char *mime) {  
  3. 。。。  
  4. mime = tmp.string();  
  5. 。。。  
  6. //拿到音频文件的格式,放到mime里。  
  7. MediaExtractor *ret = NULL;  
  8.     if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)  
  9.             || !strcasecmp(mime, "audio/mp4")) {  
  10.         ret = new MPEG4Extractor(source);  
  11.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG) ||  
  12.             !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_BP3)) {ALOGD("shitao.li----mp3extroctor");  
  13.         ret = new MP3Extractor(source, meta);  
  14.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)  
  15.             || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)  
  16.             || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR)) {  
  17.         ret = new AMRExtractor(source);  
  18.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {  
  19.         ret = new FLACExtractor(source);  
  20.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV) ||  
  21.                !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV2)) {  
  22.         ret = new WAVExtractor(source);  
  23.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG) ||  
  24.                !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG2)) {  
  25.         ret = new OggExtractor(source);  
  26.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA) ||  
  27.                !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WEBM)) {  
  28.         ret = new MatroskaExtractor(source);  
  29.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {  
  30.         ret = new MPEG2TSExtractor(source);  
  31.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI) ||  
  32.                !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MSVIDEO)) {  
  33.         ret = new AVIExtractor(source);  
  34.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WVM)) {  
  35.         // Return now.  WVExtractor should not have the DrmFlag set in the block below.  
  36.         return new WVMExtractor(source);  
  37.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC_ADTS) ||  
  38.                !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC2)) {  
  39.         ret = new AACExtractor(source, meta);  
  40.     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS)) {  
  41.         ret = new MPEG2PSExtractor(source);  
  42.     } else {  
  43.     if (isDrm) {  
  44.         ret = new MPEG4Extractor(source);  
  45.     }  
  46.     }  
  47. 。。。。。。  
  48.  }  

使用mime得到的类型和提供的类型作对比,就找到队形的extroctor了。

From: http://blog.csdn.net/shi_xin/article/details/42085775

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值