我正在尝试使用ExoPlayer播放AAC流。我遇到了使用内置的MediaPlayer时遇到的相同问题:在KitCat上启动需要15到20秒的时间(可能在其他版本上也是如此,但是现在没有任何可测试的东西)。

在ExoPlayer上,我可以获得一些有关背景情况的信息。

流程卡在MediaExtractor.getDataSource()FrameworkSampleDataSource方法prepare

extractor = new MediaExtractor();
extractor.setDataSource(context, uri, headers);


日志中提到了有关readAt方法超时的内容

01-12 12:07:03.458    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ mDisconnectAtHighwatermark = 0, cacheConfig is NULL(1)
01-12 12:07:03.463    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:03.463    4537-4615/com.example.claudio.exopla I/ChromiumHTTPDataSource﹕ readAt offset =0 , mCurrentOffset = 0 size = 65536
01-12 12:07:04.298    4537-4615/com.example.claudio.exopla D/HTTPBase﹕ [1] Network BandWidth = 0 Kbps
01-12 12:07:04.298    4537-4615/com.example.claudio.exopla D/NuCachedSource2﹕ Remaining (64K), HighWaterThreshold (20480)
01-12 12:07:04.298    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:04.298    4537-4615/com.example.claudio.exopla I/ChromiumHTTPDataSource﹕ readAt offset =65536 , mCurrentOffset = 65536 size = 65536
01-12 12:07:04.298    4537-4584/com.example.claudio.exopla V/ChromiumHTTPDataSource﹕ mContentSize is undefined or network might be disconnected
01-12 12:07:04.298    4537-4584/com.example.claudio.exopla V/ChromiumHTTPDataSource﹕ mContentSize is undefined or network might be disconnected
01-12 12:07:04.323    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:06.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:06.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:06.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:08.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:08.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:08.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:10.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:10.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:10.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:12.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:12.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:12.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:14.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:14.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:14.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:16.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:16.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:16.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:18.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:18.328    4537-4584/com.example.claudio.exopla E/NuCachedSource2﹕ readAt:Timeout ( player case )
01-12 12:07:18.328    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: mIsMetadataRetriever == 0
01-12 12:07:18.413    4537-4615/com.example.claudio.exopla D/HTTPBase﹕ [2] Network BandWidth = 70 Kbps
01-12 12:07:18.413    4537-4615/com.example.claudio.exopla D/NuCachedSource2﹕ Remaining (127K), HighWaterThreshold (20480)
01-12 12:07:18.413    4537-4615/com.example.claudio.exopla D/NuCachedSource2﹕ readInternal - late!!!
01-12 12:07:18.418    4537-4584/com.example.claudio.exopla I/NuCachedSource2﹕ readAt: waiting end ( player case )
01-12 12:07:18.418    4537-4615/com.example.claudio.exopla I/ChromiumHTTPDataSource﹕ readAt offset =131072 , mCurrentOffset = 131072 size = 65536
01-12 12:07:18.423    4537-4584/com.example.claudio.exopla E/tmiMPEG2TSExtractor﹕ SnifftmiMPEG2TS @LINE=1372


我想这可能与缓冲有关,因为MediaPlayer也有类似的问题。这只会在真实设备上发生,模拟器会立即启动。

最佳答案

1)在完成MediaExtractor使用后,再次使null。

MediaExtractor extractor = new MediaExtractor();
 extractor.setDataSource(...);
 int numTracks = extractor.getTrackCount();
 for (int i = 0; i < numTracks; ++i) {
   MediaFormat format = extractor.getTrackFormat(i);
   String mime = format.getString(MediaFormat.KEY_MIME);
   if (weAreInterestedInThisTrack) {
     extractor.selectTrack(i);
   }
 }
 ByteBuffer inputBuffer = ByteBuffer.allocate(...)
 while (extractor.readSampleData(inputBuffer, ...) >= 0) {
   int trackIndex = extractor.getSampleTrackIndex();
   long presentationTimeUs = extractor.getSampleTime();
   ...
   extractor.advance();
 }

 extractor.release();
 extractor = null;


2)

MediaExtractor extractor= new MediaExtractor();
File file = new File(this.fileName);

extractor.setDataSource(this.fileName);
int tracks = extractor.getTrackCount();

extractor.selectTrack(0); ..

07-28 04:40