Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : multiple audio tracks in a VideoView?

I've got some .MP4 video files that must be read in a VideoView in an Android activity. These videos include several audio tracks, with each one corresponding to a user language (eg. : English, French, Japanese...).
I've got unexpected trouble finding any help or documentation to provide such a feature. I'm currently able to load the video and play it in a VideoView with a MediaController, but not to change audio tracks.

I'm not sure the Android SDK provides any easy way to do this, which leaves me quite clueless on how to solve my problem. I was thinking of extracting every audio track, loading the audio that I want into a MediaPlayer depending on the language, then make audio and video play together. But I fear that some sync issues could arise and prevent me from doing this.

If you have any clue, any advice to help me getting started with this problem, you're more than welcome.

like image 962
Vahron Avatar asked Jan 09 '12 13:01

Vahron


People also ask

How can I play multiple audio files on Android?

i) First, you can select a single app from the list to allow you to play sound in the background while another app is already playing audio. ii) Another option is, you can select “All apps” to turn on multi-stream audio. 4] Select your apps and you're good to go.


2 Answers

No 3rd party library required:

mVideoView.setVideoURI(Uri.parse("")); // set video source      mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {     @Override     public boolean onInfo(MediaPlayer mp, int what, int extra) {         MediaPlayer.TrackInfo[] trackInfoArray = mp.getTrackInfo();         for (int i = 0; i < trackInfoArray.length; i++) {             // you can switch out the language comparison logic to whatever works for you             if (trackInfoArray[i].getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO                 && trackInfoArray[i].getLanguage().equals(Locale.getDefault().getISO3Language()) {                 mp.selectTrack(i);                 break;             }          }          return true;     } }); 

As far as I can tell - audio tracks should be encoded in the 3-letter ISO 639-2 in order to be recognized correctly.

like image 178
Itai Hanski Avatar answered Sep 23 '22 04:09

Itai Hanski


Haven't tested myself yet, but it seems that Vitamio library has support for multiple audio tracks (among other interesting features). It is API-compatible with VideoView class from Android.

Probably you would have to use Vitamio VideoView.setAudioTrack() to set audio track (for example based on locale). See Vitamio API docs for details.

like image 22
vArDo Avatar answered Sep 25 '22 04:09

vArDo