项目中使用的是kotlin,你可以使用Java代码
引入 exoplayer完整版
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
也可引入需要版本
必选
implementation'com.google.android.exoplayer:exoplayer-core:2.X.X'
可选
implementation'com.google.android.exoplayer:exoplayer-dash:2.X.X'
implementation'com.google.android.exoplayer:exoplayer-ui:2.X.X'
启用Java 8支持
如果尚未启用,则还需要build.gradle根据ExoPlayer的要求在所有文件中打开Java 8支持 ,方法是在该android部分中添加以下 内容:
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
}
接着使用
val mUri = Uri.parse(videoUrl)
//网络srt地址
// val srtUri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt")
//本地的srt file
val srtUri = Uri.fromFile(file)
// 在播放期间测量带宽。如果不需要,可以为空。
val bandwidthMeter = DefaultBandwidthMeter.Builder(activity).build()
val trackSelector: TrackSelector = DefaultTrackSelector(AdaptiveTrackSelection.Factory())
exoPlayer = ExoPlayerFactory.newSimpleInstance(activity, trackSelector)
// 生成用于加载媒体数据的数据源实例。
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(activity,
Util.getUserAgent(activity, getString(R.string.app_name)), bandwidthMeter)
// 这是媒体资源,代表要播放的媒体。
val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mUri)
// 开发者字幕资源的格式。
val textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
null, Format.NO_VALUE, Format.NO_VALUE, Locale.getDefault().language, null, Format.OFFSET_SAMPLE_RELATIVE)
val textMediaSource: MediaSource = SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(srtUri, textFormat, C.TIME_UNSET)
val mediaSource = MergingMediaSource(videoSource, textMediaSource)
exoPlayer?.prepare(mediaSource)
mPlayerView?.player=exoPlayer
exoPlayer?.playWhenReady =true
videoUrl 可以是本地的视频,也可以是网络视频,字幕文件可以是本地的,也可以是网络的。
如果你不使用exoplayer进行播放视频,只需要字幕功能,你可以使用以下代码,然后使用exo的 SubtitleView 或者使用TextView
val file = FileUtils.writeTxtToFile(content, cacheDir.toString() + File.separator, txtName)
// val srtUri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt")
val srtUri = Uri.fromFile(file)
// 在播放期间测量带宽。如果不需要,可以为空。
val bandwidthMeter = DefaultBandwidthMeter.Builder(activity).build()
val trackSelector: TrackSelector = DefaultTrackSelector(AdaptiveTrackSelection.Factory())
exoPlayer = ExoPlayerFactory.newSimpleInstance(activity, trackSelector)
// 生成用于加载媒体数据的数据源实例。
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(activity,Util.getUserAgent(activity,getString(R.string.app_name)), bandwidthMeter)
// 开发者准备好字幕资源。
val textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
null, Format.NO_VALUE, Format.NO_VALUE, Locale.getDefault().language, null, Format.OFFSET_SAMPLE_RELATIVE)
val textMediaSource: MediaSource = SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(srtUri, textFormat, C.TIME_UNSET)
exoPlayer?.playWhenReady = true
exoPlayer?.addTextOutput {
if (it.isNullOrEmpty()) {
return@addTextOutput
}
mSubtitleLayout?.setCues(it)
// mTVSubTitle.text= it[0].text
}
exoPlayer?.prepare(textMediaSource)
exoPlayer 的播放与暂停还有seekTo需要绑定播放器的,不然addTextOutput 的监听会从一开始就执行。不会与播放器的播放监听绑定。
网友评论