我知道你的問題已經晚了,但也許答案可以幫助其他人... 我最近剛剛研究這個問題,需要改變顯示的VideView字幕的相同功能。
首先,在初始化VideoView時添加兩個字幕(source_1
和source_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());
我希望這可以幫助別人...我是長期以來一直在努力...似乎沒有太多的文件或例子...