【翻譯】安卓新播放器EXOplayer介紹

http://developer.android.com/guide/topics/media/exoplayer.htmlhtml

 
前言:

Playing videos and music is a popular activity on Android devices. The Android framework provides MediaPlayer as a quick solution for playing media with minimal code, and the MediaCodec andMediaExtractor classes are provided for building custom media players. The open source project, ExoPlayer, is a solution between these two options, providing a pre-built player that you can extend.java

在安卓設備上,咱們常常會須要播放視頻和音頻。Android framework 提供MediaPlayer 以便程序員能快速實現影音播放。同時,Android framework提供了MediaCodec 和MediaExtractor類,咱們能夠經過這兩個類來實現自定義的媒體播放器。ExoPlayer就是一個介於現有Mediaplayer和自定義媒體播放器之間的預建播放器,同時咱們經過還能夠得到比原有Mediaplayer更多的擴展能力。android

ExoPlayer supports features not currently provided by MediaPlayer, including Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming, and persistent caching. ExoPlayer can be extended to handle additional media formats, and because you include it as part of your app code, you can update it along with your app.git

ExoPlayer支持許多MediaPlayer沒有的特性,包括DASH,SmoothStreaming,已經持久緩存。ExoPlayer能夠經過進一步擴展來處理多種媒體格式,同時因爲它是內置於你的app代碼中的,因此它能夠隨着你的app來升級。程序員

This guide describes how to use ExoPlayer for playing Android supported media formats, as well as DASH and SmoothStreaming playback. This guide also discusses ExoPlayer events, messages, DRM support and guidelines for customizing the player.github

本文描述了怎麼使用ExoPlayer播放android支持的媒體格式,以及DASH和SmoothStreaming。同時本文也探討了ExoPlayer的事件、消息和DRM支持。最後本文還提供了定製player的教程。web

Note: ExoPlayer is an open source project that is not part of the Android framework and is distributed separately from the Android SDK. The project contains a library and a demo app that shows both simple and more advanced use of ExoPlayer:編程

注意:ExoPlayer 是一個開源項目,它不屬於Android framework ,而且是獨立於Android SDK獨立分發的。該項目包含一個library和一個demo,其中展現了ExoPlayer的簡單應用及其高級定製。api

  • ExoPlayer Library — This part of the project contains the core library classes.
  • Simple Demo — This part of the app demonstrates a basic use of ExoPlayer.
  • Full Demo — This part of the app demonstrates more advanced features, including the ability to select between multiple audio tracks, a background audio mode, event logging and DRM protected playback.

Overview


概述緩存

ExoPlayer is a media player built on top of the MediaExtractor and MediaCodec APIs released in Android 4.1 (API level 16). At the core of this library is the ExoPlayer class. This class maintains the player’s global state, but makes few assumptions about the nature of the media being played, such as how the media data is obtained, how it is buffered or its format. You inject this functionality through ExoPlayer’s prepare() method in the form of TrackRenderer objects.

ExoPlayer是創建在MediaExtractor and MediaCodec這兩組api之上的播放器。

這兩組api是在api 16 即Android 4.1發佈的,因此ExoPlayer的支持的最小api是api 16,可是因爲ExoPlayer的build.gradle的minSdkVersion爲9,因此在api《16時,也可使用ExoPlayer的部分功能ExoPlayer庫的核心類是ExoPlayer類。該類維護了播放器的全局狀態,同時也。好比如何獲取媒體數據,如何緩衝以及是怎樣的編碼格式。

ExoPlayer provides default TrackRenderer implementations for audio and video, which make use of theMediaCodec and AudioTrack classes in the Android framework. Both renderers require a SampleSourceobject, from which they obtain individual media samples for playback. Figure 1 shows the high level object model for an ExoPlayer implementation configured to play audio and video using these components.

ExoPlayer基於MediaCodec and AudioTrack提供了默認的音視頻的TrackRenderer實現。全部的renderers都須要SampleSource對象,ExoPlayer從SampleSource得到media samples 用於播放。圖1展現了ExoPlayer是如何配置組合這些組件用於播放音視頻的。

62518A31-B9F4-425A-B769-EAC7AF46C03C.png

Figure 1. High level object model for an ExoPlayer configured to play audio and video using TrackRenderer objects

TrackRenderer


TrackRenderer processes a component of media for playback, such as video, audio or text. The ExoPlayer class invokes methods on its TrackRenderer instances from a single playback thread, and by doing so causes each media component to be rendered as the global playback position is advanced. The ExoPlayer library provides MediaCodecVideoTrackRenderer as the default implementations rendering video andMediaCodecAudioTrackRenderer for audio. Both implementations make use of MediaCodec to decode individual media samples. They can handle all audio and video formats supported by a given Android device (see Supported Media Formats for details). The ExoPlayer library also provides an implementation for rendering text called TextTrackRenderer.

 TrackRenderer能夠處理媒體文件中的音頻、視頻以及文本。ExoPlayer會在一個單獨的播放控制線程裏調用TrackRenderer實例中的方法。這一方式使得每部分媒體組件都在播放時呈現出來ExoPlayer庫默認提供了MediaCodecVideoTrackRenderer和MediaCodecAudioTrackRenderer 類來分別實現視頻和音頻的呈現。MediaCodecVideoTrackRenderer和MediaCodecAudioTrackRenderer 類都使用了MediaCodec 來對media samples解碼。他們可以處理全部在 Supported Media Formats 列出的媒體格式。ExoPlayer還提供了TextTrackRenderer 來實現對文本的呈現。

The code example below outlines the main steps required to instantiate an ExoPlayer to play video and audio using the standard TrackRenderer implementations.

下面的代碼展現了實例化一個ExoPlayer對象並經過它播放音視頻的主要步驟。其中使用的是標準的TrackRenderer實現。


// 1. Instantiate the player.

player
=ExoPlayer.Factory.newInstance(RENDERER_COUNT);
// 2. Construct renderers.
MediaCodecVideoTrackRenderer videoRenderer =
MediaCodecAudioTrackRenderer audioRenderer =...
// 3. Inject the renderers through prepare.
player
.prepare(videoRenderer, audioRenderer);
// 4. Pass the surface to the video renderer.
player
.sendMessage(videoRenderer,MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
        surface
);
// 5. Start playback.
player
.setPlayWhenReady(true);
...
player
.release();// Don’t forget to release when done!

For a complete example, see the SimplePlayerActivity in the ExoPlayer demo app, which correctly manages an ExoPlayer instance with respect to both the Activity and Surface lifecycles.

 若是須要看完整的實例,能夠查看demo中的 SimplePlayerActivity  類。實例代碼中正確的展現了 如何在   Activity Surface  的 生命週期中正確管理 ExoPlayer實例

SampleSource


採樣源

A standard TrackRenderer implementation requires a SampleSource to be provided in its constructor. ASampleSource object provides format information and media samples to be rendered. The ExoPlayer library provides FrameworkSampleSource and ChunkSampleSource. The FrameworkSampleSource class usesMediaExtractor to request, buffer and extract the media samples. The ChunkSampleSource class provides adaptive playback using DASH or SmoothStreaming, and implements networking, buffering and media extraction within the ExoPlayer library.

標準的 TrackRenderer  實現須要在其構造函數中傳入   SampleSource對象。
一個 SampleSource 對象提供了呈現出來所須要的格式信息和採樣信息。 ChunkSampleSource 類能夠適配 DASH或 SmoothStreaming數據源,同時 ChunkSampleSource 類還實現了訪問網絡、緩衝播放以及媒體提取的能力。

Providing media using MediaExtractor

使用 MediaExtractor 提供媒體
 

In order to render media formats supported by the Android framework, the FrameworkSampleSource class usesMediaExtractor for networking, buffering and sample extraction functionality. By doing so, it supports any media container format supported by the version of Android where it is running. For more information about media formats supported by Android, see Supported Media Formats.

The diagram in Figure 2 shows the object model for an ExoPlayer implementation usingFrameworkSampleSource.

爲了呈現 Android framework,支持的媒體格式。 FrameworkSampleSource  類使用 MediaExtractor組件來訪問網絡、緩衝以及採樣提取等功能。如此一來,就能支持每一版本android所能支持的媒體格式了。關於格式支持的信息詳見:   Supported Media Formats .

圖2展現了 ExoPlayer 如何訪問 FrameworkSampleSource的對象模型

27A7B38F-F807-44F9-A74D-9C33D69C845B.png

Figure 2. Object model for an implementation of ExoPlayer that renders media formats supported by Android usingFrameworkSampleSource

The following code example outlines how the video and audio renderers are constructed to load the video from a specified URI.

下面的示例展現瞭如何從特定url構建renderer。

FrameworkSampleSource sampleSource =newFrameworkSampleSource(
        activity
, uri,null,2);
MediaCodecVideoTrackRenderer videoRenderer =newMediaCodecVideoTrackRenderer(
        sampleSource
,null,true,MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT,0,
        mainHandler
, playerActivity,50);
MediaCodecAudioTrackRenderer audioRenderer =newMediaCodecAudioTrackRenderer(
        sampleSource
,null,true);

The ExoPlayer demo app provides a complete implementation of this code in DefaultRendererBuilder. TheSimplePlaybackActivity class uses it to play one of the videos available in the demo app. Note that in the example, video and audio are muxed, meaning they are streamed together from a single URI. TheFrameworkSampleSource instance provides video samples to the videoRenderer object and audio samples to the audioRenderer object as they are extracted from the media container format. It is also possible to play demuxed media, where video and audio are streamed separately from different URIs. This functionality can be achieved by having two FrameworkSampleSource instances instead of one.

ExoPlayer demo項目裏面的DefaultRendererBuilder類中完整的包含了上面的代碼。SimplePlaybackActivity 類使用其播放了一段視頻。注意,在示例代碼中,音頻和視頻是混合在一塊兒的,這意味着視頻流和音頻流都來自於同一個uri。當採樣數據從媒體容器中從提取出來時,FrameworkSampleSource 實例把視頻採樣輸出到 videoRenderer 對象中,同時把音頻採樣輸出到 audioRenderer 對象中。固然咱們也能夠播放音頻和視頻流分開的媒體,不過這須要咱們創建兩個FrameworkSampleSource 實例。

Providing media for adaptive playback

自適應播放
 

ExoPlayer supports adaptive streaming, which allows the quality of the media data to be adjusted during playback based on the network conditions. DASH and SmoothStreaming are examples of adaptive streaming technologies. Both these approaches load media in small chunks (typically 2 to 10 seconds in duration). Whenever a chunk of media is requested, the client selects from a number of possible formats. For example, a client may select a high quality format if network conditions are good, or a low quality format if network conditions are bad. In both techniques, video and audio are streamed separately.

ExoPlayer支持自適應的數據流,也就是說,能夠在播放時根據客戶端的網絡帶寬和CPU的執行能力的改變,隨時的調整視頻質量。DASH和SmoothStreaming是這類技術的典型表明。他們都是一次一小塊一小塊地加載數據的(通常來講大概會有2到10秒的延遲)。在獲取到小塊數據以後,客戶端能夠選擇相應地格式來解析。例如,在網絡環境好的時候,客戶端能夠選擇高質量的格式,而網絡環境不佳時,能夠選擇低質量的格式。不過,這類技術裏面,其視頻流和音頻流是相互獨立的。

ExoPlayer supports adaptive playback through use of the ChunkSampleSource class, which loads chunks of media data from which individual samples can be extracted. Each ChunkSampleSource requires aChunkSource object to be injected through its constructor, which is responsible for providing media chunks from which to load and read samples. The DashMp4ChunkSource and SmoothStreamingChunkSource classes provide DASH and SmoothStreaming playback using the FMP4 container format. The DashWebMChunkSourceclass uses the WebM container format to provide DASH playback.

ExoPlayer經過ChunkSampleSource 類來實現適應性播放功能。每一個ChunkSampleSource 對象都須要在構造時注入一個ChunkSource 對象。咱們經過ChunkSource 對象來加載採樣數據,並輸出成ChunkSampleSource 類須要的小塊媒體數據。DashMp4ChunkSource 類和SmoothStreamingChunkSource 類分別提供了DASH和SmoothStreaming協議下的解決方案,不過這兩個類支持的都是FMP4格式。DashWebMChunkSource類 則實現了DASH協議下使用WebM格式解析的解決方案。

All of the standard ChunkSource implementations require a FormatEvaluator and a DataSource to be injected through their constructors. The FormatEvaluator objects select from the available formats before each chunk is loaded. The DataSource objects are responsible for actually loading the data. Finally, theChunkSampleSources require a LoadControl object that controls the chunk buffering policy.

全部的標準ChunkSource 實現都須要在構造器中注入一個FormatEvaluator 對象 和一個 DataSource 對象。

The object model of an ExoPlayer configured for a DASH adaptive playback is shown in the diagram below. This example uses an HttpDataSource object to stream the media over the network. The video quality is varied at runtime using the adaptive implementation of FormatEvaluator, while audio is played at a fixed quality level。FormatEvaluator 對象會在每一個數據塊被加載以前選擇可用的格式。  DataSource 對象負責實際加載數據。

最後,ChunkSampleSources 須要一個LoadControl 對象來控制數據塊的緩衝策略。

The object model of an ExoPlayer configured for a DASH adaptive playback is shown in the diagram below. This example uses an HttpDataSource object to stream the media over the network. The video quality is varied at runtime using the adaptive implementation of FormatEvaluator, while audio is played at a fixed quality level.

下圖展現瞭如何實現DASH 自適應播放。此例中經過HttpDataSource 對象從網絡獲取數據流。使用FormatEvaluator 時,音頻流的質量是固定的,而視頻流的質量則是在運行時不斷變化的。

1FBCD1E8-F65A-430A-A301-06C8B0861E3B.png

Figure 3. Object model for a DASH adaptive playback using ExoPlayer

The following code example outlines how the video and audio renderers are constructed.

下面的代碼標出了音視頻的renders是如何被構造出來的。

Handler mainHandler = playerActivity.getMainHandler();
LoadControl loadControl =newDefaultLoadControl(
       
newBufferPool(BUFFER_SEGMENT_SIZE));
BandwidthMeter bandwidthMeter =newBandwidthMeter();

// Build the video renderer.
DataSource videoDataSource =newHttpDataSource(userAgent,
       
HttpDataSource.REJECT_PAYWALL_TYPES, bandwidthMeter);
ChunkSource videoChunkSource =newDashMp4ChunkSource(videoDataSource,
       
newAdaptiveEvaluator(bandwidthMeter), videoRepresentations);
ChunkSampleSource videoSampleSource =newChunkSampleSource(videoChunkSource,
        loadControl
, VIDEO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE,true);
MediaCodecVideoTrackRenderer videoRenderer =newMediaCodecVideoTrackRenderer(
        videoSampleSource
,null,true,MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT,
       
0, mainHandler, playerActivity,50);

// Build the audio renderer.
DataSource audioDataSource =newHttpDataSource(userAgent,
       
HttpDataSource.REJECT_PAYWALL_TYPES, bandwidthMeter);
ChunkSource audioChunkSource =newDashMp4ChunkSource(audioDataSource,
       
newFormatEvaluator.FixedEvaluator(), audioRepresentation);
SampleSource audioSampleSource =newChunkSampleSource(audioChunkSource,
        loadControl
, AUDIO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE,true);
MediaCodecAudioTrackRenderer audioRenderer =newMediaCodecAudioTrackRenderer(
        audioSampleSource
,null,true);

In this code, videoRepresentations and audioRepresentation are Representation objects, each of which describes one of the available media streams. In the DASH model, these streams are parsed from a media presentation description (MPD) file. The ExoPlayer library provides aMediaPresentationDescriptionParser class to obtain Representation objects from MPD files.

在這段代碼中,videoRepresentations 和 audioRepresentation 是Representation 對象。它們各自表示了一種可用的媒體流。在DASH模型中,這些數據流從MPD文件中轉換而來,這一轉換過程經過MediaPresentationDescriptionParser 實現。

Note: Building Representation objects from MPD files is not required. You can build Representation objects from other data sources if necessary.

注意:注意不必定要從MPD中構建Representation對象。若是必要的話,你也能夠從其餘數據源建立Representation對象。

The ExoPlayer demo app provides complete implementation of this code in DashVodRendererBuilder. TheSimplePlaybackActivity class uses this builder to construct renderers for playing DASH sample videos in the demo app. It asynchronously fetches a specified MPD file in order to construct the requiredRepresentation objects. For an equivalent SmoothStreaming example, see theSmoothStreamingRendererBuilder class in the demo app.

ExoPlayer demo項目在 DashVodRendererBuilder 類中徹底實現了這段代碼。SimplePlaybackActivity使用DashVodRendererBuilder 構造了renders用以播放DASH樣例視頻。在demo中,異步獲取了指定的MPD文件,來構建Representation 對象。一樣地,demo中使用SmoothStreamingRendererBuilder 來實現相同的功能。

Format selection for adaptive playback

自適應播放時的格式選擇
 

For DASH and SmoothStreaming playback, consider both static format selection at the start of playback and dynamic format selection during playback. Static format selection should be used to filter out formats that should not be used throughout the playback, for example formats with resolutions higher than the maximum supported by the playback device. Dynamic selection varies the selected format during playback, typically to adapt video quality in response to changes in network conditions.

使用DASH和SmoothStreaming進行自適應播放時,能夠分爲兩種狀況。一種是在播放開始時指定一個格式,這被成爲靜態格式選擇。一種是在播放過程當中動態切換播放格式,稱之爲動態格式選擇。靜態格式選擇通常用於不能在播放過程當中隨意改變格式的狀況,好比格式大於設備能夠支持的最大分辨率的時候。動態格式選擇能夠在播放時改變格式,最典型的就是當網絡環境改變時,相應地改變視頻格式。

Static format selection
靜態格式選擇
 

When preparing a player, you should consider filtering out some of the available formats if they are not useable for playback. Static format selection allows you to filter out formats that cannot be used on a particular device or are not compatible with your player. For audio playback, this often means picking a single format to play and discarding the others.

當播放器進行準備操做時,咱們須要把一些不用的格式給過濾出去。好比那些不被當前設備所支持或者播放器不兼容的格式。對於音頻播放來講,這一般意味着選定一個格式。

 

For video playback, filtering formats can be more complicated. Apps should first eliminate any streams that whose resolution is too high to be played by the device. For H.264, which is normally used for DASH and SmoothStreaming playback, ExoPlayer’s MediaCodecUtil class provides a maxH264DecodableFrameSize()method that can be used to determine what resolution streams the device is able to handle, as shown in the following code example:

對視頻播放來講,過濾格式會稍微複雜一些。首先應用要評估視頻的分辨率是否是高過設備所能處理的分辨率。H.264這一格式一般在DASH和SmoothStreaming中採用,ExoPlayer的MediaCodecUtil 類專門提供了一個maxH264DecodableFrameSize()方法。該方法用於判斷設備能處理哪一分辨率的視頻。示例以下:

int maxDecodableFrameSize =MediaCodecUtil.maxH264DecodableFrameSize();
Format format = representation.format;
if(format.width * format.height <= maxDecodableFrameSize){
 
// The device can play this stream.
  videoRepresentations
.add(representation);
}else{
 
// The device isn't capable of playing this stream.
}

This approach is used to filter Representations in the DashVodRendererBuilder class of the ExoPlayer demo app, and similarly to filter track indices in SmoothStreamingRendererBuilder.

上面的代碼展現瞭如何過濾Representations。這段代碼在demo項目中的DashVodRendererBuilder 類能夠看到。

SmoothStreamingRendererBuilder 也能夠看到用相似的方法過濾track。

In addition to eliminating unsupported formats, it should be noted that the ability to seamlessly switch between H.264 streams of different resolution is an optional decoder feature available in Android 4.3 (API level 16) and higher, and so is not supported by all devices. The availability of an adaptive H.264 decoder can be queried using MediaCodecUtil, as shown in the following code example:

H.264和其餘格式之間的無縫切換須要在Android 4.3 (API level 16) 及以上版本才支持。

以下示例,咱們能夠經過MediaCodecUtil 類來判斷適配性。

boolean isAdaptive =MediaCodecUtil.getDecoderInfo(MimeTypes.VIDEO_H264).adaptive;

The MediaCodecVideoTrackRenderer class is still able to handle resolution changes on devices that do not have adaptive decoders, however the switch is not seamless. Typically, the switch creates a small discontinuity in visual output lasting around 50-100ms. For devices that do not provide an adaptive decoder, app developers may choose to adapt between formats at a single fixed resolution so as to avoid discontinuities. The ExoPlayer demo app implementation does not pick a fixed resolution.

13811046323

MediaCodecVideoTrackRenderer 類仍然能夠在沒有自適應解碼器的設備上處理分辨率的變化,可是切換過程並非無縫的。這個切換會引起50-100ms的卡頓。若是要在沒有自適應解碼器的設備上避免卡頓,開發者能夠選擇使用固定的的分辨率。不過ExoPlayer裏面並無這一示例。

Dynamic format selection
動態格式選擇
 

During playback, you can use a FormatEvaluator to dynamically select from the available video formats. The ExoPlayer library provides a FormatEvaluator.Adaptive implementation for dynamically selecting between video formats based on the current network conditions.

經過使用FormatEvaluator,咱們能夠在播放時動態選擇可用的視頻格式。ExoPlayer庫提供了FormatEvaluator.Adaptive的一個實現來根據當前網絡情況選擇格式。

This class provides a simple, general purpose reference implementation, however you are encouraged to write your own FormatEvaluator implementation to best suit your particular needs.

這個類提供了一個簡單通用的實現。可是仍是建議你根據本身的需求實現你本身的FormatEvaluator

Player Events 

播放器事件


During playback, your app can listen for events generated by the ExoPlayer that indicate the overall state of the player. These events are useful as triggers for updating the app user interface such as playback controls. Many ExoPlayer components also report their own component specific low level events, which can be useful for performance monitoring.

ExoPlayer提供了不少事件,便於用戶監聽ExoPlayer的全部狀態。在播放控制時,經過這些事件能夠及時的觸發ui更新。許多ExoPlayer組件還會將一些底層的事件暴露出來,這在性能監控時會很是有用。

High level events

高級事件

ExoPlayer allows instances of ExoPlayer.Listener to be added and removed using its addListener() andremoveListener() methods. Registered listeners are notified of changes in playback state, as well as when errors occur that cause playback to fail. For more information about the valid playback states and the possible transitions between them, see the ExoPlayer source code.

ExoPlayer中能夠經過addListener() 和removeListener() 方法,添加和刪除ExoPlayer.Listener 實例。被註冊的監聽者會接收到播放時的狀態變化以及異常。若是想進一步瞭解播放狀態的細節,還有狀態之間的轉換等內容,請參看ExoPlayer的源代碼。

Developers who implement custom playback controls should register a listener and use it to update their controls as the player’s state changes. An app should also show an appropriate error to the user if playback fails.

開發者在實現自定義播放控制時,能夠經過註冊listener事件監聽器的方式來更新ui控件。固然,若是播放器出現異常,app也須要將友好的錯誤信息呈現出來。

Low level events

低級事件

 

In addition to high level listeners, many of the individual components provided by the ExoPlayer library allow their own event listeners. For example, MediaCodecVideoTrackRenderer has constructors that take aMediaCodecVideoTrackRenderer.EventListener. In the ExoPlayer demo app, SimplePlayerActivityacts as a listener so that it can adjust the dimensions of the target surface to have the correct height and width ratio for the video being played:

做爲高級事件的補充,ExoPlayer的各個獨立組件也會有他們本身的事件監聽機制。好比MediaCodecVideoTrackRenderer 類就有一個接受MediaCodecVideoTrackRenderer.EventListener的構造函數。

在demo應用中,SimplePlayerActivity實際上扮演了一個監聽者的角色。因此它能夠根據播放器的事件來調整surface的大小以適應視頻流。

@Override
publicvoid onVideoSizeChanged(int width,int height){
  surfaceView
.setVideoWidthHeightRatio(height ==0?1:(float) width / height);
}

The RendererBuilder classes in the ExoPlayer demo app inject the activity as the listener, for example in theDashVodRendererBuilder class:

在demo應用中,RendererBuilder 類注入了activity做爲監聽者。代碼以下:

MediaCodecVideoTrackRenderer videoRenderer =newMediaCodecVideoTrackRenderer(
        videoSampleSource
,null,true,MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT,
       
0,mainHandler, playerActivity,50);

Note that you must pass a Handler object to the renderer, which determines the thread on which the listener’s methods are invoked. In most cases, you should use a Handler associated with the app’s main thread, as is the case in this example.

須要注意的是,你須要傳遞一個Handler 對象給renderer。這決定了監聽者的方法在那個線程上調用。大部分狀況下你能夠直接像本例同樣,傳入主線程的handler。

Listening to individual components can be useful for adjusting UI based on player events, as in the example above. Listening to component events can also be helpful for logging performance metrics. For example,MediaCodecVideoTrackRenderer notifies its listener of dropped video frames. A developer may wish to log such metrics to track playback performance in their app.

如上述實例所示,監聽獨立組件的事件使得根據事件調整ui會更方便。監聽組件的事件有助於記錄性能指標。例如,MediaCodecVideoTrackRenderer 能夠發送丟棄的視頻幀數給監聽者。開發者能夠經過記錄這些指標來追蹤app的性能。

Many components also notify their listeners when errors occur. Such errors may or may not cause playback to fail. If an error does not cause playback to fail, it may still result in degraded performance, and so you may wish to log all errors in order to track playback performance. Note that an ExoPlayer instance always notifies its high level listeners of errors that cause playback to fail, in addition to the listener of the individual component from which the error originated. Hence, you should display error messages to users only from high level listeners. Within individual component listeners, you should use error notifications only for informational purposes.

 

許多組件在發生異常時,也會發出通知,有些異常會致使播放失敗,有些不會。那些不會致使播放失敗的異常也很重要,極可能會影響應用的性能,因此咱們也會須要記錄全部的異常信息,以便追蹤。

須要注意的是,ExoPlayer實例發送給高級監聽者的異常,一般都是致使播放失敗的異常。若是是高級監聽者中接受到得異常,咱們最後將錯誤信息告訴用戶。而對於獨立組件的監聽者來講,咱們只須要對錯誤信息進行記錄便可。

Sending messages to components

發送消息給組件


Some ExoPlayer components allow changes in configuration during playback. By convention, you make these changes by passing asynchronous messages through the ExoPlayer to the component. This approach ensures both thread safety and that the configuration change is executed in order with any other operations being performed on the player.

一些ExoPlayer組件容許在播放過程當中改變配置。爲了方便起見,可使用發送異步消息的方式變動配置。這樣能夠確保線程安全,同時也能保證配置的更改按順序執行。

The most common use of messaging is passing a target surface to MediaCodecVideoTrackRenderer:

最多見的用法就是更改視頻顯示的surface:

player.sendMessage(videoRenderer,MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
        surface
);

Note that if the surface needs to be cleared because SurfaceHolder.Callback.surfaceDestroyed() has been invoked, then you must send this message using the blocking variant of sendMessage():

注意,若是SurfaceHolder.Callback.surfaceDestroyed() 被調用即surface被銷燬時,咱們須要發送一個阻塞消息給EXOPlayer:

 

player.blockingSendMessage(videoRenderer,
       
MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,null);

You must use a blocking message because the contract of surfaceDestroyed() requires that the app does not attempt to access the surface after the method returns. The SimplePlayerActivity class in the demo app demonstrates how the surface should be set and cleared.

這麼作的緣由是由於,surface被銷燬時,須要確保MediaCodecVideoTrackRenderer 不會再將數據寫入已經釋放的surface。

在demo應用中,有相關代碼能夠展現如何正確地設置和清理surface。

Customizing ExoPlayer


One of the main benefits of ExoPlayer over MediaPlayer is the ability to customize and extend the player to better suit the developer’s use case. The ExoPlayer library is designed specifically with this in mind, defining a number of abstract base classes and interfaces that make it possible for app developers to easily replace the default implementations provided by the library. Here are some use cases for building custom components:

使用ExoPlayer的一個主要的好處是ExoPlayer可以更加容易的擴展和定製化。ExoPlayer定義了許多朝向基類以及接口。這時代開發者可以很方便的使用本身實現來替換默認實現:

  • TrackRenderer - You may want to implement a custom TrackRenderer to handle media types other than audio and video. The TextTrackRenderer class within the ExoPlayer library is an example of how to implement a custom renderer. You could use the approach it demonstrates to render custom overlays or annotations. Implementing this kind of functionality as a TrackRenderer makes it easy to keep the overlays or annotations in sync with the other media being played.
  • TrackRenderer - 咱們可能會想要定製TrackRenderer 來處理視頻和音頻以外的其餘媒體。TextTrackRenderer 就是一個自定義TrackRenderer -的實例。
  • SampleSource - If you need to support a container format not already handled by MediaExtractor or ExoPlayer, consider implementing a custom SampleSource class.
  • SampleSource - 若是你想要支持一個在MediaExtractor 或者 ExoPlayer的默認支持範圍以外的容器格式。能夠考慮實現自定義的SampleSource類。
  • FormatEvaluator - The ExoPlayer library provides FormatEvaluator.Adaptive as a simple reference implementation that switches between different quality video formats based on the available bandwidth. App developers are encouraged to develop their own adaptive FormatEvaluator implementations, which can be designed to suit their use specific needs.
  • FormatEvaluator - ExoPlayer library provides FormatEvaluator.Adaptive as
  • DataSource - ExoPlayer’s upstream package already contains a number of DataSource implementations for different use cases, such as writing and reading to and from a persistent media cache. You may want to implement you own DataSource class to load data in another way, such as a custom protocol or HTTP stack for data input.
  • DataSource - ExoPlayer的上級包名下已經包含了一系列DataSource 實現,好比從媒體緩存池讀寫數據等。
  • 你能夠實現你本身的DataSource 類,以別的方式加載數據,好比自定義的協議或者HTTP stack 。

Custom component guidelines自定義組件的一些守則

If a custom component needs to report events back to the app, we recommend that you do so using the same model as existing ExoPlayer components, where an event listener is passed together with a Handler to the constructor of the component.

若是你得自定義組件須要有事件交互,咱們推薦你使用和已有的ExoPlayer組件相同的編程模型。把 Handler 和事件監聽者經過構造器傳入組件中去。

We recommended that custom components use the same model as existing ExoPlayer components to allow reconfiguration by the app during playback, as described in Sending messages to components. To do this, you should implement a ExoPlayerComponent and receive configuration changes in its handleMessage()method. Your app should pass configuration changes by calling ExoPlayer’s sendMessage() andblockingSendMessage() methods.

對於組件的設置更改,咱們也推薦你同已有的模型保持一致。不過這樣的話,你須要實現ExoPlayerComponent ,在handleMessage()方法中接受配置的變動。按照這種模式實現的自定義組件,就能夠和默認組件同樣,經過調用sendMessage() 和blockingSendMessage() 方法來變動配置了。

Digital Rights Management

數字版權管理


On Android 4.3 (API level 18) and higher, ExoPlayer supports Digital Rights Managment (DRM) protected playback. In order to play DRM protected content with ExoPlayer, your app must inject a DrmSessionManagerinto the MediaCodecVideoTrackRenderer and MediaCodecAudioTrackRenderer constructors. ADrmSessionManager object is responsible for providing the MediaCrypto object required for decryption, as well as ensuring that the required decryption keys are available to the underlying DRM module being used.

在安卓4.3及以上系統中,ExoPlayer能夠支持DRM技術。可是爲了播放drm內容,你須要將一個DrmSessionManager 注入到MediaCodecVideoTrackRenderer 和 MediaCodecAudioTrackRenderer 構造器中。DrmSessionManager 對象提供了一個用於解密的MediaCrypto 對象,固然也要確保解密的keys是有效的。

The ExoPlayer library provides a default implementation of DrmSessionManager, calledStreamingDrmSessionManager, which uses MediaDrm. The session manager supports any DRM scheme for which a modular DRM component exists on the device. All Android devices are required to support Widevine modular DRM (with L3 security, although many devices also support L1). Some devices may support additional schemes such as PlayReady.

ExoPlayer庫提供了一個默認的DrmSessionManager 實現,叫作StreamingDrmSessionManager。StreamingDrmSessionManager 有用到MediaDrm. StreamingDrmSessionManager 支持任意一種設備支持的DRM架構。基本上,全部的安卓設備都支持Widevine modular DRM。也有一些設備能支持其餘架構。

The StreamingDrmSessionManager class requires a MediaDrmCallback to be injected into its constructor, which is responsible for actually making provisioning and key requests. You should implement this interface to make network requests to your license server and obtain the required keys. TheWidevineTestMediaDrmCallback class in the ExoPlayer demo app sends requests to a Widevine test server.

使用StreamingDrmSessionManager 類時,須要注入一個MediaDrmCallback 到構造器中。MediaDrmCallback 用於獲取祕鑰。你須要實現這個接口,並經過網絡鏈接你的受權服務器來獲取密鑰。demo項目中的WidevineTestMediaDrmCallback 類就實現了經過網絡鏈接 Widevine測試服務器以獲取密鑰。

相關文章
相關標籤/搜索