如何更改媒体播放器中的字幕

时间:2014-12-11 05:57:41

标签: android android-mediaplayer 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;
        }

1 个答案:

答案 0 :(得分:1)

查看state diagram。在使用新关联的资源再次初始化之前,请尝试reset()release() mediaPlayer