提前感谢。。我正在视频播放器应用程序中实现音轨功能我成功地加载了曲目并显示在对话框中,但我的问题是,当我试图更改曲目时,曲目没有更改。请查看我的代码,有人能告诉我在哪里犯了错误吗?
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoViewSeekBar.setMax(videoView.getDuration());
mediaPlayer.start();
checkMultiAudioTrack(mediaPlayer);
}
});
这是方法
private void checkMultiAudioTrack(MediaPlayer mediaPlayer) {
MediaPlayer.TrackInfo trackInfos[] = mediaPlayer.getTrackInfo();
ArrayList<Integer> audioTracksIndex = new ArrayList<>();
for (int i = 0; i < trackInfos.length; i++) {
if (trackInfos[i].getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
audioTracksIndex.add(i);
}
}
if (trackInfos.length > 2) {
AlertDialog.Builder builder = new AlertDialog.Builder(VideoPlayerActivity.this);
builder.setTitle("Audio Tracks");
String values[] = new String[audioTracksIndex.size()];
for (int i = 0; i < audioTracksIndex.size(); i++) {
values[i] = String.valueOf("Track " + i);
}
/*
* SingleChoice means RadioGroup
* */
builder.setSingleChoiceItems(values, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mediaPlayer.selectTrack(which);
Toast.makeText(VideoPlayerActivity.this, "Track " + which + " Selected", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
}
我没有测试它,但看起来您在selectTrack
中放置了错误的索引。which
是带名称的值的索引,但您需要保存在audioTracksIndex
中的轨迹索引。换句话说,用selectTrack(audioTracksIndex.get(which))
来改变selectTrack(which)
。
如果不起作用,请检查您的状态是否正确,以调用selectTrack。