管理多個曲目
默認情況下,沒有字幕文件,只有一個音軌被下載。要下載與視頻相關的其他資產,請按照以下步驟操作:
-
獲取視頻雲的參考離線目錄客戶。
private OfflineCatalog catalog; ... catalog = new OfflineCatalog(this, eventEmitter, ACCOUNT_ID, POLICY_KEY); ... -
致電getMediaFormatTracksAvailable方法,傳入視頻您要獲取其信息的對象,以及MediaFormatListener監聽返回的數據。
OfflineCatalog.getMediaFormatTracksAvailable( @NonNull final Video video, @NonNull final MediaDownloadable.MediaFormatListener mediaFormatListener) -
的
MediaFormatListener調用以下方法:void onResult(MediaDownloadable mediaDownloadable, Bundle mediaFormatBundle);的可下載的媒體對象包含信息,包括視頻格式,音頻語言和字幕。的
mediaDownloadable對像被緩存,並在您調用時使用OfflineCatalog.downloadVideo(Video)。的束對象包含有關可用媒體格式軌道的信息,並具有以下屬性:
- MediaDownloadable.VIDEO_RENDITIONS -包含一個數組列表
MediaFormat對象 - MediaDownloadable.AUDIO_LANGUAGES -包含一個數組列表
MediaFormat對象。 - MediaDownloadable.AUDIO_LANGUAGE_ROLES -包含字符串數組列表。此列表提供了有關
MediaDownloadable.AUDIO_LANGUAGES。索引與音頻語言列表匹配。音頻角色的示例可以是主要角色,也可以是替代角色。 - MediaDownloadable.CAPTIONS -包含一個數組列表
MediaFormat對象
- MediaDownloadable.VIDEO_RENDITIONS -包含一個數組列表
-
從包中檢索數據列表。以下是檢索不同列表的示例:
ArrayList<MediaFormat> video = bundle.getParcelableArrayList(MediaDownloadable.VIDEO_RENDITIONS); ArrayList<MediaFormat> audio = bundle.getParcelableArrayList(MediaDownloadable.AUDIO_LANGUAGES); ArrayList<String> roles = bundle.getStringArrayList(MediaDownloadable.AUDIO_LANGUAGE_ROLES); ArrayList<MediaFormat> captions = bundle.getParcelableArrayList(MediaDownloadable.CAPTIONS); -
創建一個新的
Bundle(filteredBundle)以選擇要包括在下載內容中的音軌和隱藏式字幕。Bundle filteredBundle = new Bundle(); -
從先前創建的列表中過濾所需的曲目,然後將其添加到新曲目中
filteredBundle。ArrayList<MediaFormat> captions = bundle.getParcelableArrayList(MediaDownloadable.CAPTIONS); if (captions != null && captions.size() > 0) { ArrayList<MediaFormat> newCaptions = new ArrayList<>(); newCaptions.add(captions.get(0)); filteredBundle.putParcelableArrayList(MediaDownloadable.CAPTIONS, newCaptions); } -
設置
filteredBundle到MediaDownloadable目的。mediaDownloadable.setConfigurationBundle(filteredBundle); -
下載視頻。
offlineCatalog.downloadVideo(video);在內部,
OfflineCatalog將使用緩存的MediaDownloadable由...提供MediaDownloadable.MediaFormatListener打回來。
程式碼範例
有關下載其他音軌和字幕文件的完整示例,請參見離線播放示例應用。