Android2.3时引入流媒体框架,而流媒体框架的核心是NuPlayer。Android4.0之后HttpLive和RTSP协议开始使用NuPlayer播放器,Android5.0(L版本)之后本地播放也开始使用NuPlayer播放器。
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/sdcard/test.mp3");
mp.prepare();
mp.start();
程序说明:
这个例子只是描述了MediaPlayer的基本使用步骤和方式,MediaPlayer还有多种使用方式和方法,并不只局限于例子所介绍的一种。具体来看:
在实现上NuPlayer和Awesomeplayer不同,NuPlayer基于StagefrightPlayer的基础类构建,利用了更底层的ALooper/AHandler机制来异步地处理请求,ALooper列队消息请求,AHandler中去处理,所以有更少的Mutex/Lock在NuPlayer中。Awesomeplayer中利用了omxcodec,而NuPlayer中利用了Acodec。
NuPlayer::Source is the parser module. Actually its interface looks like a combination of MediaExtractor and MediaSource, and it also makes seekTo as an explicit API now.
NuPlayer::Decoder connects to ACodec for AVC decoding, and to DecoderWrapper for AAC decoding, which in turn wrapps AAC software decoder in the OpenMAX style. ACodec is functionally similar as OMXCodec in Stagefright, besides the application of State pattern and passing MediaBuffers around with messages.
NuPlayer::Render is responsible for rendering audio and also controls when to post video buffers back to NativeWindow for A/V sync.
指的是Extractor(即demux)的两个通道,从这里输出的分别就是单纯的解复用后的Video和Audio流。再经过Decoder后输出的就是音、视频的输出了
- VideoRenderer + Surface即视频的输出;
- AudioSink即音频的输出;
打开Web Sequence Diagrams网址,直接粘贴进去即可。
Title Nuplayer流程
participant NuPlayerDriver
participant NuPlayer
participant GenericSource
participant FileSource
participant MediaExtractor
participant AnotherPacketSource
participant DecoderBase
participant Decoder
opt setDataSource
NuPlayerDriver->+NuPlayer: setDataSourceAsync()
NuPlayer-->GenericSource: new GenericSource()
NuPlayer-->GenericSource: setDataSource()
NuPlayer-->NuPlayer: kWhatSetDataSource
NuPlayer-->-NuPlayerDriver:
NuPlayer->+NuPlayer: kWhatSetDataSource
note right of NuPlayer: msg->findObject("source", &obj)\nmSource = static_cast<Source *>(obj.get());
NuPlayer-->-NuPlayerDriver: notifySetDataSourceCompleted()
end
opt prepare
NuPlayerDriver->+NuPlayer: prepareAsync()
NuPlayer-->NuPlayer: kWhatPrepare
NuPlayer-->-NuPlayerDriver:
NuPlayer->+NuPlayer: kWhatPrepare
NuPlayer->+GenericSource: prepareAsync()
GenericSource-->GenericSource: kWhatPrepareAsync
GenericSource-->-NuPlayer:
NuPlayer-->-NuPlayer:
GenericSource->+GenericSource: onPrepareAsync()
GenericSource-->FileSource: new FileSource()
GenericSource->GenericSource:initFromDataSource()
GenericSource-->AnotherPacketSource: new AnotherPacketSource()
GenericSource-->FileSource: sniff()
GenericSource-->MediaExtractor:MediaExtractor::Create()
GenericSource-->-NuPlayer: finishPrepareAsync()
end
opt Start
NuPlayerDriver->+NuPlayer: start()
NuPlayer-->NuPlayer: kWhatStart
NuPlayer-->-NuPlayerDriver:
NuPlayer->+NuPlayer: onStart()
NuPlayer->+GenericSource: start()
GenericSource->GenericSource: postReadBuffer()
GenericSource-->GenericSource: kWhatReadBuffer
GenericSource->GenericSource: onReadBuffer()
GenericSource->GenericSource:readBuffer()
GenericSource-->AnotherPacketSource: queueAccessUnit()
GenericSource-->GenericSource: kWhatStart
GenericSource-->GenericSource: restartPollBuffering()
GenericSource-->-NuPlayer:
NuPlayer->+DecoderBase: setRenderer()
DecoderBase-->DecoderBase: kWhatSetRenderer
DecoderBase-->-NuPlayer:
NuPlayer->NuPlayer: postScanSources()
NuPlayer-->-NuPlayer: kWhatScanSources
end