16

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;
        }
Mycoola
  • 1,135
  • 1
  • 8
  • 29
user3322553
  • 239
  • 2
  • 8
  • 2
    I personnaly recommand to not use MediaPlayer anymore to use ExoPlayer https://github.com/google/ExoPlayer# Developed by Google, this is the future of video on Android. It support subtitles too. – Hugo Gresse Nov 25 '15 at 12:41
  • Since you presumably don't want to restart the whole object as suggested in @Mibit's answer, have you tried having the time text sources combined and just have them as different tracks, instead of separate resources? – craigts Dec 02 '15 at 05:09

1 Answers1

1

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

Mibit
  • 316
  • 1
  • 10