Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change subtitle in mediaplayer

I can assign subtitle once but after that if I tried to change subtitle. It won't accept new one, it display previous one every-time.

Below code I'm using to change subtitle.

 MediaPlayer mediaPlayer;

    public void changeSubtitle()
    {
    //disable subtitle if it has
    if(textTrackIndex!=0){    
    mediaPlayer.deselectTrack(textTrackIndex);
    textTrackIndex=0;
    mediaPlayer.setOnTimedTextListener(null);
    }

    //try to asssign new subtitle

                mediaPlayer
                        .addTimedTextSource(
                                (Environment.getExternalStorageDirectory()
                                        .getPath()
                                        + "/"+"filename",
                                MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
    int textTrackIndex = findTrackIndexFor(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT,mediaPlayer.getTrackInfo());

            if (textTrackIndex >= 0) {
                mediaPlayer.selectTrack(textTrackIndex);
            } else {
                Log.w(TAG, "Cannot find text track!");
            }
            mediaPlayer.setOnTimedTextListener(this);
    }

    private int findTrackIndexFor(int mediaTrackType, TrackInfo[] trackInfo) 
    {
            int index = -1;
            for (int i = 0; i < trackInfo.length; i++) {
                if (trackInfo[i].getTrackType() == mediaTrackType) {
                    return i;
                }
            }
            return index;
        }
like image 444
user3322553 Avatar asked Dec 11 '14 05:12

user3322553


People also ask

How do I change the subtitle font in MPC?

MPC Options -> Subtitles -> Default Style. There you can choose the font, colors, and font size.

How do I change subtitles in VLC player?

Go to Tools → Preferences (set Show Settings to All) and Video → Subtitles/OSD → Text renderer and adjust anything you want.


1 Answers

Have a look at the state diagram. Try to reset() and release() your mediaPlayer before you initialize it again with newly associated resources.

like image 89
Mibit Avatar answered Oct 24 '22 21:10

Mibit