Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exoplayer - Enable subtitle on video

I want to add subtitle on video from srt (or vtt or ttml).

What I have made till now - We have an api by which we get youtube video url with the subtitle file link on our own server. I extract video url from the youtube link with the help of a library and then play that video on exoplayer.

Till now its working 100%.

Now we have to add subtitles over the video, for this I download the subtitle file from for the video from our server and save it in internal memory, and while creating the MediaSource for exoplayer we set the subtitle file.

I have tried almost every thing on the internet but not able to set subtitle on the video. I am sharing my code here with XML and java file, also the code how I am setting the subtitle file.

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>
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);

Thanks in advance.

like image 607
Amit Pandey Avatar asked Oct 15 '25 15:10

Amit Pandey


1 Answers

I can see possibly two problems with your code. The first is C.SELECTION_FLAG_AUTOSELECT instead of C.SELECTION_FLAG_DEFAULT. The second is possibly problem with SRT (or TTLM) file Uri. Check Logcat if there is no message like Resources$NotFoundException or similar from ExoPlayer. It can be little bit tricky to create this Uri. In any case below is minimal working example. Files are in assets directory inside project. For XML layout is used 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()

Gradle dependencies:

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"
like image 193
Warlock Avatar answered Oct 17 '25 06:10

Warlock