2016-11-07 51 views
0

我有多種語言,用於單獨文件中的字幕。我想在看電影時改變字幕。在Android VideoView中使用addSubtitleSource()方法更改字幕的正確方法是什麼?如何在VideoView中更改字幕

if (subtitle.equals("Language_1")){ //button "Language_1" 
mVideoView.addSubtitleSource(source_1, MediaFormat.createSubtitleFormat("text/vtt", "en")); 
} 
if (subtitle.equals("Language2")){ //button "Language_2" 
mVideoView.addSubtitleSource(source_2, MediaFormat.createSubtitleFormat("text/vtt", "en")); 
} 

如果我停止和啓動視頻,那麼我可以字幕之間,這個碼開關(大概是因爲重新啓動視頻已經「遺忘」關於加分源後)。有沒有辦法卸載一個字幕源,並添加不同的字幕源,而無需重新加載視頻?我正在考慮像removeSubtitleSource()這樣的方法,它不存在。

編輯:也許我可以使用CaptioningManger爲此?

回答

0

我知道你的問題已經晚了,但也許答案可以幫助其他人... 我最近剛剛研究這個問題,需要改變顯示的VideView字幕的相同功能。

首先,在初始化VideoView時添加兩個字幕(source_1source_2)。

然後,你可以做的是,在你的VideoView對象的onPrepared()監聽器中,你可以迭代當前播放視頻的可用軌道,我做的是將它們保存到你的地圖上類:

所需的參考連接到您的videoView在MediaPlayer,地圖爲可用的字幕軌道和當前選中的字幕軌道是私有類成員宣佈對我的片段,包含VideoView之一:

private MediaPlayer mediaPlayer; 
private HashMap<String, IndexedTrackInfo> availableSubtitleTracks = new HashMap(); 
private IndexedTrackInfo currentSubtitleTrack; 

onPrepared listener的內容

@Override 
public void onPrepared(MediaPlayer mp) { 
    Log.d(JGA,"onPrepared"); 
    loadingIndicator.setVisibility(View.GONE); 
    videoView.start(); 
    controllerFragmentListener.notifyPlaying(); 
    isPlaying=true; 

    if(mediaPlayer == null) { 
     mediaPlayer = mp; 
    } 

    MediaPlayer.TrackInfo[] tracks = mp.getTrackInfo(); 
    Log.d(TAG, "onInfo: MediaPlayer.getTrackInfo()[" + tracks.length + "]"); 
    MediaPlayer.TrackInfo track; 
    IndexedTrackInfo indexedTrackInfo = null; 
    for (int i = 0; i < tracks.length; i++) { 
     track = tracks[i]; 
     Log.d(TAG, "onInfo: track [email protected]" + i + ": " + track.getTrackType() + " = " + track.toString()); 
     if (track.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { 
      indexedTrackInfo = new IndexedTrackInfo(track, i); 
      if(track.getFormat().containsKey(TRACK_SUBTITLE_IS_DEFAULT_KEY)) { 
       Log.d(TAG, "onInfo: Subtitle track has '" + TRACK_SUBTITLE_IS_DEFAULT_KEY + "' with value = " + track.getFormat().getInteger(TRACK_SUBTITLE_IS_DEFAULT_KEY)); 
       if(track.getFormat().getInteger(TRACK_SUBTITLE_IS_DEFAULT_KEY) == 1) { 
        Log.d(TAG, "onInfo: This track set as default"); 
        currentSubtitleTrack = indexedTrackInfo; 
       } 
      } 
      availableSubtitleTracks.put(track.getLanguage(), indexedTrackInfo); 
     } 
    } 
    if(currentSubtitleTrack == null && indexedTrackInfo != null) { 
     currentSubtitleTrack = indexedTrackInfo; 
     mediaPlayer.selectTrack(currentSubtitleTrack.getIndex()); 
     Log.d(TAG, "onInfo: There was no default sub track but there are tracks available... picking the last one as default [" + currentSubtitleTrack.getIndex() + "]"); 
    } 
    controllerFragmentListener.updateSpinnerSubtitleTracks(availableSubtitleTracks); 
    controllerFragmentListener.updateSpinnerCurrentSelection(currentSubtitleTrack.getTrackInfo().getLanguage()); 
} 

而定製的POJO到asociate每個軌道與MediaPlayer的TrackInfo與其對應的索引(IndexedTrackInfo.java):

public class IndexedTrackInfo { 

    private MediaPlayer.TrackInfo trackInfo; 
    private int index; 

    public IndexedTrackInfo(MediaPlayer.TrackInfo trackInfo, int index) { 
     this.trackInfo = trackInfo; 
     this.index = index; 
    } 

    public MediaPlayer.TrackInfo getTrackInfo() { 
     return trackInfo; 
    } 

    public int getIndex() { 
     return index; 
    } 
} 

最後,我做的事是有一個Spinner顯示可用字幕給用戶,並根據其"index"更改它們。

這取決於你的需求,但對於顯示/ hidding /改變當前字幕軌,你需要做的是一樣的東西:

//To show the subtitles: 
mediaPlayer.selectTrack(currentSubtitleTrack.getIndex()); 
//To hide the subtitles: 
mediaPlayer.deselectTrack(currentSubtitleTrack.getIndex()); 
//To change the track (secondSourceSubtitleTrack is the object in the map corresponding to your other subtitle source): 
currentSubtitleTrack = secondSourceSubtitleTrack; 
mediaPlayer.selectTrack(currentSubtitleTrack.getIndex()); 

我希望這可以幫助別人...我是長期以來一直在努力...似乎沒有太多的文件或例子...