ExoPlayer详解——媒体类型(官方文档)

ExoPlayer详解——媒体类型(官方文档)

2023年7月11日发(作者:)

ExoPlayer详解——媒体类型(官⽅⽂档)ExoPlayer详解系列⽂章⼀、DASHExoPlayer⽀持多种容器格式的DASH。必须对媒体流进⾏解复⽤,这意味着必须在DASH清单中的不同AdaptationSet元素中定义视频,⾳频和⽂本(CEA-608是⼀个例外,如下表所述)。还必须⽀持包含的⾳频和视频样本格式(有关详细信息,请参阅 部分)。特征集装箱FMP4WebMMatroskaMPEG-TS隐藏式字幕/字幕TTMLWebVTTCEA-608元数据EMSG元数据内容保护WidevinePlayReady SL2000ClearKey是是是API 19+(“cenc”⽅案)和25+(“cbcs”,“cbc1”和“cens”⽅案)仅限Android TVAPI 21+是嵌⼊在FMP4中是是是根据ISO / IEC 14496-30原始或嵌⼊FMP4中根据ISO / IEC 14496-30原始或嵌⼊FMP4中携带在FMP4视频流中嵌⼊的SEI消息是是是否仅拆分流仅拆分流仅拆分流没有⽀持计划⽀持评论1、创建MediaSource要播放DASH流,请DashMediaSource像往常⼀样创建并准备播放器。 // Create a data source factory. y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a DASH media source pointing to a DASH manifest uri. MediaSource mediaSource = new y(dataSourceFactory) .createMediaSource(dashUri); // Create a player instance. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the DASH media source. e(mediaSource);ExoPlayer将⾃动调整清单中定义的表⽰,同时考虑可⽤带宽和设备功能。2、访问清单您可以通过调⽤rentManifest来检索当前清单。对于DASH,您应该将返回的对象强制转换为DashManifest。该onTimelineChanged回调istener也被称为清单加载。对于点播内容,这将发⽣⼀次,对于直播内容可能会发⽣多次。下⾯的代码⽚段显⽰了应⽤程序在加载清单时如何执⾏操作。 tener( new istener() { @Override public void onTimelineChanged( Timeline timeline, @Nullable Object manifest, @neChangeReason int reason) { if (manifest != null) { DashManifest dashManifest = (DashManifest) manifest; // Do something with the manifest. } } });3、侧载清单对于特定⽤例,有⼀种替代⽅法可以通过将DashManifest对象传递给构造函数来提供清单。 y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a dash media source with a dash manifest. MediaSource mediaSource = new y(dataSourceFactory) .createMediaSource(dashManifest); // Create a player instance which gets an adaptive track selector by default. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the dash media source. e(mediaSource);4、⾃定义DASH播放ExoPlayer提供多种⽅式让您根据应⽤的需求定制播放体验。以下部分简要介绍了构建DashMediaSource时可⽤的⼀些⾃定义选项。有关更多常规⾃定义选项,请参阅。(1)、⾃定义服务器交互某些应⽤可能希望拦截HTTP请求和响应。您可能希望注⼊⾃定义请求标头,读取服务器的响应标头,修改请求的URI等。例如,您的应⽤程序可以通过在请求媒体段时将标记作为标头注⼊来验证⾃⾝。您可以通过将⾃定义注⼊到你创建的DashMediaSource中来实现这些⾏为。以下代码段显⽰了标头注⼊的⽰例: DashMediaSource dashMediaSource = new y( () -> { HttpDataSource dataSource = new DefaultHttpDataSource(userAgent); // Set a custom authentication request header. uestProperty("Header", "Value"); return dataSource; }) .createMediaSource(dashUri);(2)、⾃定义错误处理实现⾃定义允许应⽤程序⾃定义ExoPlayer对加载错误的反应⽅式。例如,应⽤程序可能希望快速失败⽽不是多次重试,或者可能想要⾃定义控制player在每次重试之间等待多长时间的退避逻辑。以下代码段显⽰了在创建DashMediaSource以下内容时如何实现⾃定义退避逻辑: DashMediaSource dashMediaSource = new y(dataSourceFactory) .setLoadErrorHandlingPolicy( new DefaultLoadErrorHandlingPolicy() { @Override public long getRetryDelayMsFor(...) { // Implement custom back-off logic here. } }) .createMediaSource(dashUri);您可以在我们的媒体帖⼦中找到有关的更多信息。5、BehindLiveWindowException在播放具有有限可⽤性的直播流的情况下,如果播放器暂停或缓冲⾜够长的时间段,则播放器可能落后于该实况窗⼝。在这种情况下,抛出⼀个BehindLiveWindowException,可以捕获它并在现场边缘恢复player。演⽰应⽤程序的举例说明了这种⽅法。 @Override public void onPlayerError(ExoPlaybackException e) { if (isBehindLiveWindow(e)) { // Re-initialize player at the live edge. } else { // Handle other errors } }

private static boolean isBehindLiveWindow(ExoPlaybackException e) { if ( != _SOURCE) { return false; } Throwable cause = rceException(); while (cause != null) { if (cause instanceof BehindLiveWindowException) { return true; } cause = se(); } return false; }⼆、HLSExoPlayer⽀持多种容器格式的HLS。还必须⽀持包含的⾳频和视频样本格式(有关详细信息,请参阅 ⽰例格式部分)。我们强烈⿎励HLS内容制作产⽣⾼品质的HLS流,描述 在这⾥。特征集装箱MPEG-TSFMP4 / CMAFADTS(AAC)MP3隐藏式字幕/字幕CEA-608WebVTT是是是是是是⽀持评论元数据特征ID3元数据内容保护AES-128样品AES-128WidevinePlayReady SL2000⽀持是评论是没有是是API 19+(“cenc”⽅案)和25+(“cbcs”⽅案)仅限Android TV1、创建MediaSource要播放HLS流,请像往常⼀样创建HlsMediaSource并准备播放器。 // Create a data source factory. y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a HLS media source pointing to a playlist uri. HlsMediaSource hlsMediaSource = new y(dataSourceFactory).createMediaSource(uri); // Create a player instance. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the HLS media source. e(hlsMediaSource);URI传递给MediaSource()可以指向媒体播放列表或主播放列表。如果URI指向声明多个#EXT-X-STREAM-INF标签的主播放列表,则ExoPlayer将⾃动调整变体,同时考虑可⽤带宽和设备功能。2、⾃定义HLS播放ExoPlayer提供多种⽅式让您根据应⽤的需求定制播放体验。以下部分简要介绍了构建HlsMediaSource时可⽤的⼀些⾃定义选项。有关更多常规⾃定义选项,请参阅。(1)、启⽤更快的启动时间通过启⽤⽆块准备,您可以显着提⾼HLS启动时间。当您启⽤⽆块准备并且#EXT-X-STREAM-INF标签包含该

CODECS属性时,ExoPlayer将避免在准备过程中下载媒体段。以下代码段显⽰了如何启⽤⽆块准备: HlsMediaSource hlsMediaSource = new y(dataSourceFactory) .setAllowChunklessPreparation(true) .createMediaSource(hlsUri);您可以在我们的关于⽆框准备的媒体帖⼦中找到更多详细信息。(2)、⾃定义服务器交互某些应⽤可能希望拦截HTTP请求和响应。您可能希望注⼊⾃定义请求标头,读取服务器的响应标头,修改请求的URI等。例如,您的应⽤程序可以通过在访问媒体段的URI中注⼊⾃定义标记来验证⾃⾝。您可以通过将⾃定义注⼊到创建的HlsMediaSource中来实现这些⾏为。以下代码段显⽰了请求标头注⼊的⽰例: HlsMediaSource hlsMediaSource = new y( dataType -> { HttpDataSource dataSource = new DefaultHttpDataSource(userAgent); if (dataType == _TYPE_MEDIA) { // The data source will be used for fetching media segments. We // set a custom authentication request header. uestProperty("Header", "Value"); } return dataSource; }) .createMediaSource(hlsUri);(3)、⾃定义错误处理实现⾃定义允许应⽤程序⾃定义ExoPlayer对加载错误的反应⽅式。例如,应⽤程序可能希望快速失败⽽不是多次重试,或者可能想要⾃定义控制player在每次重试之间等待多长时间的退避逻辑。以下代码段显⽰了在创建以下HlsMediaSource内容时如何实现⾃定义退避逻辑: HlsMediaSource hlsMediaSource = new y(dataSourceFactory) .setLoadErrorHandlingPolicy( new DefaultLoadErrorHandlingPolicy() { @Override public long getRetryDelayMsFor(...) { // Implement custom back-off logic here. } }) .createMediaSource(hlsUri);您可以在我们的媒体帖⼦中找到有关。3、创建⾼质量的HLS内容为了充分利⽤ExoPlayer,您可以遵循某些指导原则来改进您的HLS内容。阅读我们,以获得完整的解释。要点是:使⽤精确的段持续时间。使⽤连续媒体流; 避免跨段的媒体结构的变化。使⽤#EXT-X-INDEPENDENT-SEGMENTS标签。更喜欢分离的流,⽽不是包含视频和⾳频的⽂件。包括主播放列表中的所有信息。以下指南专门适⽤于直播流:使⽤#EXT-X-PROGRAM-DATE-TIME标签。使⽤#EXT-X-DISCONTINUITY-SEQUENCE标签。提供⼀个长时间存在的窗⼝。⼀分钟或时间越长越好。4、BehindLiveWindowException在播放具有有限可⽤性的直播流的情况下,如果播放器暂停或缓冲⾜够长的时间段,则播放器可能落后于该实况窗⼝。在这种情况下,抛出⼀个BehindLiveWindowException,可以捕获它并在现场边缘恢复player。演⽰应⽤程序的举例说明了这种⽅法。 @Override public void onPlayerError(ExoPlaybackException e) { if (isBehindLiveWindow(e)) { // Re-initialize player at the live edge. } else { // Handle other errors } }

private static boolean isBehindLiveWindow(ExoPlaybackException e) { if ( != _SOURCE) { return false; } Throwable cause = rceException(); while (cause != null) { if (cause instanceof BehindLiveWindowException) { return true; } cause = se(); } return false; }三、SmoothtreamingExoPlayer⽀持使⽤FMP4容器格式的SmoothStreaming。必须对媒体流进⾏解复⽤,这意味着必须在SmoothStreaming清单中的不同StreamIndex元素中定义视频,⾳频和⽂本。还必须⽀持包含的⾳频和视频样本格式(有关详细信息,请参阅 部分)。特征集装箱FMP4隐藏式字幕/字幕TTML内容保护PlayReady SL2000是仅限Android TV是嵌⼊在FMP4中是仅拆分流⽀持评论1、创建媒体源要播放SmoothStreaming流,请像往常⼀样使⽤它创建SsMediaSource并准备播放器。 // Create a data source factory. y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a SmoothStreaming media source pointing to a manifest uri. MediaSource mediaSource = new y(dataSourceFactory).createMediaSource(ssUri); // Create a player instance. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the SmoothStreaming media source. e(mediaSource);ExoPlayer将⾃动调整清单中定义的流,同时考虑可⽤带宽和设备功能。2、访问清单您可以通过调⽤来检索当前清单rentManifest。对于SmoothStreaming,您应该将返回的对象强制转换为SsManifest。该 onTimelineChanged回调istener每当清单加载也被称为。对于点播内容,这将发⽣⼀次,对于直播内容可能会发⽣多次。下⾯的代码⽚段显⽰了应⽤程序在加载清单时如何执⾏操作。 tener( new istener() { @Override public void onTimelineChanged( Timeline timeline, @Nullable Object manifest, @neChangeReason int reason) { if (manifest != null) { SsManifest ssManifest = (SsManifest) manifest; // Do something with the manifest. } } });3、侧载清单对于特定⽤例,有⼀种替代⽅法可以通过将SsManifest对象传递给构造函数来提供清单。 y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a smooth streaming media source with a smooth streaming manifest. MediaSource mediaSource = new y(dataSourceFactory).createMediaSource(ssManifest); // Create a player instance which gets an adaptive track selector by default. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the media source. e(mediaSource);4、⾃定义SmoothStreaming回放ExoPlayer提供多种⽅式让您根据应⽤的需求定制播放体验。以下部分简要介绍了构建SsMediaSource时可⽤的⼀些⾃定义选项。有关更多常规⾃定义选项,请参阅。(1)、⾃定义服务器交互某些应⽤可能希望拦截HTTP请求和响应。您可能希望注⼊⾃定义请求标头,读取服务器的响应标头,修改请求的URI等。例如,您的应⽤程序可以通过在访问媒体段的URI中注⼊⾃定义标记来验证⾃⾝。您可以通过将⾃定义注⼊到创建的SsMediaSource中来实现这些⾏为。以下代码段显⽰了请求标头注⼊的⽰例:SsMediaSource ssMediaSource = new y( () -> { HttpDataSource dataSource = new DefaultHttpDataSource(userAgent); // Set a custom authentication request header. uestProperty("Header", "Value"); return dataSource; }) .createMediaSource(ssUri);(1)、⾃定义错误处理实现⾃定义允许应⽤程序⾃定义ExoPlayer对加载错误的反应⽅式。例如,应⽤程序可能希望快速失败⽽不是多次重试,或者可能想要⾃定义控制player在每次重试之间等待多长时间的退避逻辑。以下代码段显⽰了在创建以下SsMediaSource内容时如何实现⾃定义退避逻辑: SsMediaSource ssMediaSource = new y(dataSourceFactory) .setLoadErrorHandlingPolicy( new DefaultLoadErrorHandlingPolicy() { @Override public long getRetryDelayMsFor(...) { // Implement custom back-off logic here. } }) .createMediaSource(ssUri);您可以在我们的媒体帖⼦中找到有关的更多信息。5、BehindLiveWindowException在播放具有有限可⽤性的直播流的情况下,如果播放器暂停或缓冲⾜够长的时间段,则播放器可能落后于该实况窗⼝。在这种情况下,抛出⼀个BehindLiveWindowException,可以捕获它并在现场边缘恢复player。演⽰应⽤程序的举例说明了这种⽅法。@Overridepublic void onPlayerError(ExoPlaybackException e) { if (isBehindLiveWindow(e)) { // Re-initialize player at the live edge. } else { // Handle other errors }}private static boolean isBehindLiveWindow(ExoPlaybackException e) { if ( != _SOURCE) { return false; } Throwable cause = rceException(); while (cause != null) { if (cause instanceof BehindLiveWindowException) { return true; } cause = se(); } return false;四、Progressive以下容器格式的流可以由ExoPlayer直接播放。还必须⽀持包含的⾳频和视频样本格式(有关详细信息,请参阅 部分)。容器格式MP4M4AFMP4WebMMatroskaMP3OggWAVMPEG-TS⽀持是是是是是是是是是有些流只能使⽤恒定⽐特率寻求**包含Vorbis,Opus和Flac评论MPEG-PS容器格式FLVADTS(AAC)FlacAMR是⽀持是是是是评论不可寻求*只能使⽤恒定⽐特率寻求**仅使⽤只能使⽤恒定⽐特率寻求***寻求不受⽀持,因为容器不提供元数据(例如,样本索引)以允许媒体播放器以有效的⽅式执⾏搜索。如果需要寻求,我们建议使⽤更合适的容器格式。**这些提取器具有FLAG_ENABLE_CONSTANT_BITRATE_SEEKING⽤于使⽤恒定⽐特率假设来实现近似搜索的标志。默认情况下不启⽤此功能。最简单的⽅法来启⽤此功能⽀持它的是使⽤所有提取

stantBitrateSeekingEnabled,描述 。1、创建MediaSource要播放渐进流,请像往常⼀样创建ProgressiveMediaSource并准备播放器。 // Create a data source factory. y dataSourceFactory = new DefaultHttpDataSourceFactory(rAgent(context, "app-name")); // Create a progressive media source pointing to a stream uri. MediaSource mediaSource = new y(dataSourceFactory) .createMediaSource(progressiveUri); // Create a player instance. SimpleExoPlayer player = pleInstance(context); // Prepare the player with the progressive media source. e(mediaSource);2、⾃定义渐进式播放ExoPlayer提供多种⽅式让您根据应⽤的需求定制播放体验。以下部分简要介绍了构建ProgressiveMediaSource时可⽤的⼀些⾃定义选项。有关更多常规⾃定义选项,请参阅。(1)、设置提取器标志提取器标志可⽤于控制如何提取单个格式。它们可以设置为

DefaultExtractorsFactory,然后可以在实例化时使⽤y。以下⽰例传递⼀个标志,该标志禁⽤MP4流的编辑列表解析。 DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory() .setMp4ExtractorFlags(_WORKAROUND_IGNORE_EDIT_LISTS); ProgressiveMediaSource progressiveMediaSource = new y(dataSourceFactory, extractorsFactory) .createMediaSource(progressiveUri);(2)、启⽤恒定⽐特率搜索对于MP3,ADTS和AMR流,您可以使⽤带FLAG_ENABLE_CONSTANT_BITRATE_SEEKING标志的恒定⽐特率假设启⽤近似搜索。可以使⽤上述⽅法为各个提取器设置这些标志。要为所有⽀持它的提取器启⽤恒定⽐特率搜索,请使⽤stantBitrateSeekingEnabled。DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory().setConstantBitrateSeekingEnabled(true);ProgressiveMediaSource progressiveMediaSource = new y(dataSourceFactory, extractorsFactory) .createMediaSource(progressiveUri);(3)、⾃定义服务器交互某些应⽤可能希望拦截HTTP请求和响应。您可能希望注⼊⾃定义请求标头,读取服务器的响应标头,修改请求的URI等。例如,您的应⽤程序可以通过在请求媒体段时将标记作为标头注⼊来验证⾃⾝。您可以通过将⾃定义注⼊到ProgressiveMediaSourcecreate中来实现这些⾏为。以下代码段显⽰了标头注⼊的⽰例:ProgressiveMediaSource progressiveMediaSource = new y( () -> { HttpDataSource dataSource = new DefaultHttpDataSource(userAgent); // Set a custom authentication request header. uestProperty("Header", "Value"); return dataSource; }) .createMediaSource(progressiveUri);(1)、⾃定义错误处理实现⾃定义允许应⽤程序⾃定义ExoPlayer对加载错误的反应⽅式。例如,应⽤程序可能希望快速失败⽽不是多次重试,或者可能想要⾃定义控制玩家在每次重试之间等待多长时间的退避逻辑。以下代码段显⽰了在创建以下ProgressiveMediaSource内容时如何实现⾃定义退避逻辑 :ProgressiveMediaSource progressiveMediaSource = new y(dataSourceFactory) .setLoadErrorHandlingPolicy( new DefaultLoadErrorHandlingPolicy() { @Override public long getRetryDelayMsFor(...) { // Implement custom back-off logic here. } }) .createMediaSource(progressiveUri);您可以在我们的媒体帖⼦中找到有关的更多信息。您的关注和点赞是我分享的动⼒,如有帮助请勿吝啬!ヽ( ̄▽ ̄)

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689068605a202609.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信