我正在使用 ExoPlayer 在 RecyclerView 中流式传输视频。
我正在 ViewHolder 内的 RecyclerView 适配器的绑定方法中实现 ExoPlayer。
我使用的视频格式是 m3u8,我使用的 URL 在浏览器中有效。
所以我知道视频链接是有效的。我还在那里测试了 YouTube 链接。
这是来自 Recyclerview 适配器的 ViewHolder 的代码 ->
class ViewHolder private constructor(val binding: FeedRowBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(context: Activity){
if (entity.content.videolink != null) {
setupVideoPlayer(entity.content.videolink, context)
}
}
}
private fun setupVideoPlayer(url: String, context: Activity) {
val videoExoPlayer = SimpleExoPlayer.Builder(binding.videoView.context).build()
videoExoPlayer.prepare(createUrlMediaSource(url, context))
binding.play.setOnClickListener {
videoExoPlayer.playWhenReady = true
}
binding.pause.setOnClickListener {
videoExoPlayer.playWhenReady = false
}
}
private fun createUrlMediaSource(url: String, context: Activity): MediaSource {
val userAgent = Util.getUserAgent(context, context.getString(R.string.about))
return ProgressiveMediaSource
.Factory(DefaultDataSourceFactory(context, userAgent), DefaultExtractorsFactory())
.createMediaSource(Uri.parse(url))
}
加载我的 recyclerview 并进入包含视频的行时,出现以下错误:
ExoPlayerImplInternal:源错误。
com.google.android.exoplayer2.source.UnrecognizedInputFormatException:
没有可用的提取器(MatroskaExtractor、
FragmentedMp4Extractor、Mp4Extractor、Mp3Extractor、AdtsExtractor、
Ac3Extractor、TsExtractor、FlvExtractor、OggExtractor、PsExtractor、
WavExtractor、AmrExtractor、Ac4Extractor、FlacExtractor)可以读取
溪流。
在 com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractorHolder.selectExtractor(ProgressiveMediaPeriod.java:1090)
在 com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:969)
在 com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
在 java.lang.Thread.run(Thread.java:764)
我不确定我做错了什么,关于如何解决这个问题有什么想法吗?