Exoplayer: Change Subtitle in MergingMediaSource / HlsMediaSource (Need More Information)

Created on 6 May 2019  路  5Comments  路  Source: google/ExoPlayer

Is there a way to change the subtitle ? The rest of the subject-related (subtitles) do not provide enough information (#3886) ! how to change subtitle ? i can't find any solution in all issue this repository and document

public class VideoPlayer extends AppCompatActivity {
    SimpleExoPlayer player;
    PlayerView playerView;
    ImageButton subtitleButton;

    MappingTrackSelector selector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.video_player);
        Activity activity = this;
        subtitleButton = findViewById(R.id.videoPlayer_subitle);
        subtitleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                renderDialog();
            }
        });
        this.initialVideo();
    }

    public void renderDialog() {
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.subtitle_layout);
        dialog.getWindow().setLayout(1000, 600);
        TextView textView = dialog.findViewById(R.id.titleSubHeader);
        textView.setText("夭蹖乇 賳賵蹖爻");

        RadioGroup radioGroup = dialog.findViewById(R.id.radioGroupSubtitle);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == 0) {
                    dialog.dismiss();
                } else {

                    //Change subtitle way//
                    DefaultTrackSelector trackSelector = new DefaultTrackSelector();
                    trackSelector
                            .buildUponParameters()
                            .setPreferredTextLanguage("en").build();
                   //Change subtitle way//

                    //Hide subtitle//
                    //playerView.getSubtitleView().setVisibility(View.GONE);
                    //Hide subtitle//
                    dialog.dismiss();
                }
            }
        });
        for (int i = 0; i < 2; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
            radioButton.setText("subtitle" + i);
            radioButton.setButtonDrawable(getResources().getDrawable(R.drawable.ic_subtitles));
            radioGroup.addView(radioButton);

        }
        dialog.show();
    }


    public void initialVideo() {
        try {
            playerView = findViewById(R.id.player_view);
            Uri url=Uri.parse("https://cinemamarket.arvanvod.com/M8VEn7Pd57/f4a067f36fbba952899b07caa2bc1526/1557212738/kanP6qVDl3/h_,144_200,240_400,360_800,480_1500,720_2500,1080_2963,k.mp4.list/master.m3u8?secure=true");
            Uri urlSubtitleFa = Uri.parse("https://cinemamarket.arvanvod.com/M8VEn7Pd57/0fe9353212a0abe5c5fd1d319125c346/1557212800/NaZ2Xw1opA/fa_CKTzdIfGcRGJTPUOD1ei4aXAIKLCjUhhmHhFmZ08.vtt");
            Uri urlSubtitleEn = Uri.parse("https://cinemamarket.arvanvod.com/M8VEn7Pd57/342c17e7625ba13fddd11c0ffdd2e384/1557212295/kanP6qVDl3/fa_SMYXp8jsyHGZEGTmclozXW2jKD6PNUQ53b6Fa3ZE.vtt");


            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                    Util.getUserAgent(this, "novinCinemaVideo"));
            MediaSource videoSource = new HlsMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(url);

            Format textFormatFa = Format.createTextSampleFormat(null, MimeTypes.TEXT_VTT,
                    null, Format.NO_VALUE, Format.NO_VALUE, "fa", null, 
            Format.OFFSET_SAMPLE_RELATIVE);

            Format textFormatEn = Format.createTextSampleFormat(null, MimeTypes.TEXT_VTT,
                    null, Format.NO_VALUE, Format.NO_VALUE, "en", null, 
            Format.OFFSET_SAMPLE_RELATIVE);

            MediaSource subtitleSourceFA =
                    new SingleSampleMediaSource.Factory(dataSourceFactory)
                            .createMediaSource(urlSubtitleFa, textFormatFa, C.TIME_UNSET);

            MediaSource subtitleSourceEn =
                    new SingleSampleMediaSource.Factory(dataSourceFactory)
                            .createMediaSource(urlSubtitleEn, textFormatEn, C.TIME_UNSET);

            MediaSource mediaSource = new MergingMediaSource(videoSource, subtitleSourceFA, 
            subtitleSourceEn);
            playerView.setPlayer(player);
            player.prepare(mediaSource);
            player.setPlayWhenReady(true);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


}
question

Most helpful comment

This code doesn't actually do anything:

trackSelector
    .buildUponParameters()
    .setPreferredTextLanguage("en").build();

You need to actually set the built parameters back on the selector:

DefaultTrackSelector.Parameters newParameters = trackSelector
    .buildUponParameters()
    .setPreferredTextLanguage("en").build();
trackSelector.setParameters(newParameters);

All 5 comments

You can select tracks by using the DefaultTrackSelector. There is a page around track selection on the ExoPlayer website.

You can build upon the existing parameters and use setPreferredTextLanguage(lang).

@marcbaechinger i use DefaultTrackSelector in my code but not change subtitle

DefaultTrackSelector trackSelector = new DefaultTrackSelector();
                    trackSelector
                            .buildUponParameters()
                            .setPreferredTextLanguage("en").build();

I'd recommend using an EventLogger in your app. This will get you some logs around which tracks are available when you start playing the video. With the information printed to the console it may be easier to check what tracks are available and can be selected.

This code doesn't actually do anything:

trackSelector
    .buildUponParameters()
    .setPreferredTextLanguage("en").build();

You need to actually set the built parameters back on the selector:

DefaultTrackSelector.Parameters newParameters = trackSelector
    .buildUponParameters()
    .setPreferredTextLanguage("en").build();
trackSelector.setParameters(newParameters);

@ojw28 thank you , you are save my life :)

Was this page helpful?
0 / 5 - 0 ratings