Ami*_*dey 4 java android android-video-player exoplayer exoplayer2.x
我想在 srt(或 vtt 或 ttml)视频上添加字幕。
到目前为止我所做的 - 我们有一个 api,通过它我们可以在我们自己的服务器上获取带有字幕文件链接的 youtube 视频 url。我在库的帮助下从 youtube 链接中提取视频 url,然后在 exoplayer 上播放该视频。
到目前为止它的工作率100%。
现在我们必须在视频上添加字幕,为此我从我们的服务器下载视频的字幕文件并将其保存在内部存储器中,在为 exoplayer 创建 MediaSource 时我们设置字幕文件。
我几乎尝试了互联网上的所有方法,但无法在视频上设置字幕。我在这里用 XML 和 java 文件分享我的代码,以及我如何设置字幕文件的代码。
activity_video.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#D9000000"
android:orientation="vertical"
android:gravity="center"
tools:context=".Activities.Video_Activity"
android:keepScreenOn="true">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_below="@+id/video_navigation_bar"
android:background="@android:color/transparent"
app:controller_layout_id="@layout/layout_exoplayer_control_views">
<ProgressBar
android:id="@+id/video_buffer_indicator"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:translationZ="@dimen/_51sdp"
android:visibility="gone" />
</com.google.android.exoplayer2.ui.PlayerView>
Run Code Online (Sandbox Code Playgroud)
MediaItem.SubtitleConfiguration subtitle =
new MediaItem.SubtitleConfiguration.Builder(Uri.parse(filePath))
.setMimeType(MimeTypes.APPLICATION_TTML) // The correct MIME type (required).
.setLanguage("en-US") // The subtitle language (optional).
.setSelectionFlags(C.SELECTION_FLAG_AUTOSELECT) // Selection flags for the track (optional).
.build();
MediaItem mediaItem =
new MediaItem.Builder()
.setUri(String.valueOf(mediaSource.get("video_link")))
.setSubtitleConfigurations(ImmutableList.of(subtitle))
.build();
list.add((new ProgressiveMediaSource.Factory(factory)).createMediaSource(mediaItem));
exoPlayer.setMediaSources(list);
exoPlayer.prepare();
exoPlayer.seekTo(currentlyPlayingIndex, C.TIME_UNSET);
gc.onBoardingPlayListStatus.get(currentlyPlayingIndex).replace("watching", true);
videoAdapter.notifyItemChanged(currentlyPlayingIndex);
exoPlayer.setPlayWhenReady(true);
exoPlayer.play();
videoPlayer.getSubtitleView().setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我可以看到您的代码可能存在两个问题。第一个是 C.SELECTION_FLAG_AUTOSELECT 而不是 C.SELECTION_FLAG_DEFAULT。第二个可能是 SRT(或 TTLM)文件 Uri 的问题。检查 Logcat 是否没有来自 ExoPlayer 的类似 Resources$NotFoundException 或类似消息。创建此 Uri 可能有点棘手。无论如何,下面是最小的工作示例。文件位于项目内的 asset 目录中。对于 XML 布局,使用 com.google.android.exoplayer2.ui.StyledPlayerView。
val playerView = view.findViewById<StyledPlayerView>(R.id.video_pv)
val exoPlayer = ExoPlayer.Builder(requireActivity()).build()
playerView.player = exoPlayer
val assetSrtUri = Uri.parse(("file:///android_asset/subtitle.srt"))
val subtitle = SubtitleConfiguration.Builder(assetSrtUri)
.setMimeType(MimeTypes.APPLICATION_SUBRIP)
.setLanguage("en")
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
.build()
val assetVideoUri = Uri.parse(("file:///android_asset/video.mp4"))
val mediaItem = MediaItem.Builder()
.setUri(assetVideoUri)
.setSubtitleConfigurations(ImmutableList.of(subtitle))
.build()
exoPlayer.setMediaItem(mediaItem)
exoPlayer.prepare()
exoPlayer.play()
Run Code Online (Sandbox Code Playgroud)
梯度依赖:
implementation "com.google.android.exoplayer:exoplayer:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-core:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-ui:2.17.1"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6913 次 |
| 最近记录: |