自定义ExoPlayer MediaSource-从哪里开始? [英] Custom ExoPlayer MediaSource -- where to start?

查看:198
本文介绍了自定义ExoPlayer MediaSource-从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ExoPlayer创建自定义媒体播放器(我之前在同一主题上曾提出过几个问题,因为我对Android开发非常陌生,似乎我碰到了很多麻烦,我写的代码).

I'm working on creating a custom media player using ExoPlayer (I've previously opened several questions on the same topic, because I'm very new to Android development and it seems like I run into a wall with every line of code I write).

作为这个自定义播放器的一部分,我想下载,解析和处理我们的业务为定义我们的内容而生成的XML文件.该XML文件提供了网络ID的网址(向内容所有者投放广告的4-6秒视频),内容的网址以及用于播放插播前和插播广告的广告标记URL.

As part of this custom player I would like to download, parse, and handle an XML file that our business produces to define our content. This XML file gives a URL for a network ID (a 4-6 second video advertising the owner of the content), a URL for the content, and an ad tag URL for playing pre-roll and mid-roll ads.

我的目标是将此XML文件作为视频源传递给 prepare(),调用 setPlayWhenReady(true),然后让所有内容按预期进行播放(网络ID,内容和广告)

My goal is to pass this XML file as a video source to prepare(), call setPlayWhenReady(true), then have everything play as expected (network ID, content, and ads)

为此,我相信我需要创建一个自定义MediaSource,但是我找不到任何很好的文档或教程. MediaSources上的ExoPlayer文档在这种情况下实际上是无用的,仅描述了如何利用ConcatenatingMediaSource,MergingMediaSource,然后使用LoopingMediaSource自定义媒体的播放.

To do this I believe I need to create a custom MediaSource -- however I cannot find any good documentation or tutorials on doing so. The ExoPlayer documentation on MediaSources is practically useless on this case, only describing how to utilize ConcatenatingMediaSource, MergingMediaSource, and LoopingMediaSource to customize the playback of media.

继续自己研究,可以使用定制的Extractor来实现我想要的功能.当我将内容传递给ExtractorMediaSource时,出现错误 com.google.android.exoplayer2.source.UnrecognizedInputFormatException:没有可用的提取器(MatroskaExtractor,FragmentedMp4Extractor,Mp4Extractor,Mp3Extractor,AdtsExtractor,Ac3Extractor,TsExtractor Flv,PsExtractor,WavExtractor,AmrExtractor)可以读取流..这让我想知道让 Extractor 解析XML,提取内容并将数据传回是否更好.我不确定这两个组件之间有什么区别,或者哪个更合适,并且缺少文档.

Continuing to research on my own, it's possible what I want may be accomplished with a custom Extractor. When I pass the content to an ExtractorMediaSource I receive the error com.google.android.exoplayer2.source.UnrecognizedInputFormatException: None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor, AmrExtractor) could read the stream.. This makes me wonder if it's better to have an Extractor parse the XML, pull the content, and pass the data back. I'm not sure yet what the difference is between these two components or which is a better fit, and the documentation is lacking.

推荐答案

因此,解析此XML文件应该由视频播放器负责,而不是由客户端的应用负责.

Therefore the parsing of this XML file should be the responsibility of the video player, not of the client's app.

因此,您实质上是在尝试创建一种新的模式,用于为基础播放器分发视频(无论如何处理).这似乎是客户端逻辑.但是,您需要一个答案,所以我将尝试给您一个答案.

So you are essentially trying to create a new schema for distributing video for an underlying player (whatever that might be, to deal with). This seems like client side logic. However, you want an answer so I'll try and give you one.

首先,根据文档,不应将 ExoPlayer 中的 Extractor 用于解析您的 XML .

First, Extractor in ExoPlayer should not be used for parsing your XML, as per the docs:

从容器格式中提取媒体数据.

Extracts media data from a container format.

这将用于从视频容器中提取视频数据,例如MP4.

This would be used for extracting the video data from a video container e.g. MP4.

在您的方案中,您可能想要查看与 DashManifestParser 类似的东西,后者使用 ParsingLoadable.Parser 来负责解析您的输入模型.然后, MediaSource 使用此 ParsingLoadable.Parser 来获取播放所需的信息.

In your scenario you probably want to look at something similar to the DashManifestParser who uses the ParsingLoadable.Parser whose responsibility it is to parse your input model. This ParsingLoadable.Parser is then used by MediaSource to get the information it needs for playback.

但是,我不建议您这样做.在这种情况下,您最好的选择是创建一个 Parser 来获取内容网址,然后将其传递给基础播放器.您的内容网址将链接到 MP4 容器,也许是DRM的内容等,但是播放器可以很好地处理所有这些操作,而无需增加所有其他复杂性.

However, I would not recommend doing any of that. The best option for you in this scenario would be to create a Parser to grab the content Url and just pass that to an underlying player. You content Url will link to an MP4 container, perhaps DRM'd content etc, but all of that can be handled by the player just fine without adding all of this other complexity.

关于创建广告,可以通过多种方式完成:

As for creating adverts, this can be done in a number of ways:

  • 只有一个播放器实例,可以在内容和广告之间交换.容易,但是您需要跟踪位置信息,并且在切换时将有缓冲.
  • 只有一个播放器实例,但使用 ConcatenatingMediaSource ,为此,您将解析 xml 来为内容和每个广告创建一个 MediaSource ,然后将它们添加到 ConcatenatingMediaSource .
  • 只有一个播放器实例,但使用 ExoPlayer 提供的 AdsLoader .这是最好的解决方案,但可悲的是缺少文档.为此,您可以提供加载广告的链接和单独的加载内容的链接.
  • Have a single player instance, swapping between content and adverts. Easy but then you need to keep track of position information and also you will have buffering when you switch.
  • Have a single player instance but use ConcatenatingMediaSource, for this you would parse the xml create a MediaSource for the content and each advert, you then add these to the ConcatenatingMediaSource.
  • Have a single player instance but use the AdsLoader provided by ExoPlayer. This is the best solution but the documentation is sadly lacking. For this you can provide a link to load adverts and a separate link to load content.

所以.总结一下.

  • 创建一个解析器,该解析器可以从XML(即内容链接和广告链接)中获取所需的信息.
  • 创建一个播放器,该播放器先为内容创建媒体源,然后为广告创建媒体源,然后将其添加到串联的媒体源中.

如果您想了解如何做某些方面,我建议您看一下使用 Exo-Player 的我们的开源库.我们甚至最近开始使用 AdsLoader . https://github.com/novoda/no-player

If you'd like to see how to do certain aspects I would suggest taking a look at our open source library that uses Exo-Player under the hood. We've even recently started to use an AdsLoader. https://github.com/novoda/no-player

这篇关于自定义ExoPlayer MediaSource-从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆