Flynn
Flynn

Reputation: 6211

AsyncTasks and Audio

I am trying to play multiple audio files, one after the other and am currently using AsyncTasks to prepare and start the mediaPlayer but have failed to find a good way to move on the to next track at the end of the current one. Not every audio file will be played every time, and it's playing is decided by a boolean value.

Any help is much apprecieated.

Upvotes: 0

Views: 952

Answers (2)

pinxue
pinxue

Reputation: 1746

I guess you have read android-sdk/docs/reference/android/media/MediaPlayer.html , it says:

When the playback reaches the end of stream, the playback completes. If the looping mode was being set to truewith setLooping(boolean), the MediaPlayer object shall remain in the Started state. If the looping mode was set to false , the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand via setOnCompletionListener(OnCompletionListener). The invoke of the callback signals that the object is now in the PlaybackCompleted state. While in the PlaybackCompleted state, calling start() can restart the playback from the beginning of the audio/video source.

So you may set a new source, prepareAsync then start in completion callback. In this way , you get continuous playback, but it is not seamless.

Upvotes: 1

chubbsondubs
chubbsondubs

Reputation: 38852

Doubtful using MediaPlayer for this will work like you want it to. Try this tutorial:

http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html

If that doesn't work you'll probably have to mix the sounds together yourself them stream that result directly to the hardware using AudioTrack. That's more low level, but it will give you the most control. It just depends on what you are doing if the AudioManager solution will work for you or not. It's definitely the simpler route. But, if you're trying to line up two samples so that when one finishes the next begins, like in a music app, you probably will have to mix and stream that audio yourself.

http://developer.android.com/reference/android/media/AudioTrack.html

Algorithm to mix sound

Upvotes: 0

Related Questions