'How to Change Audio Track in MediaPlayer
Thanks in Advance.. I am implementing audio track feature in my video player app I am Successfully load track and show in the dialog but my problem is that when I try to change the track the track was does not change have a look in my code and can anybody tell where I am making mistake
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoViewSeekBar.setMax(videoView.getDuration());
mediaPlayer.start();
checkMultiAudioTrack(mediaPlayer);
}
});
Here is the Method
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();
}
}
Solution 1:[1]
I didn't test it, but it looks that you put wrong index inside selectTrack. which is the index of the values with names, but you need the index of the tracks, saved in audioTracksIndex. In other words change the selectTrack(which) with selectTrack(audioTracksIndex.get(which)).
If this doesn't work, then check if you are in the correct state to call selectTrack.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nasko700 |
