I currently have the need to implement an application on the Android device that can open up live audio streams and play them back to the user. This is not a big deal, and I have successfully set up a streaming server and got it working for one file. My question is, however, how do I stream and play multiple audio files at once?
When I open up more than one stream (using MediaPlayer) and prepare and play each file, I only hear the audio from one file. I'm not sure this is currently possible, but I am hoping it is.
I looked into using SoundPool, but it seems to only be for local media and I MUST have this streaming as it is important for plaback speed (no wait time, etc.).
Any help or insight is greatly appreciated! Thanks in advance!开发者_StackOverflow中文版
I was tinkering with an app previously and I had a similar issue and was able to fix it except I wasn't streaming the audio. Hopefully this can help, you will need to modify it for your purposes:
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.SparseIntArray;
Instantiate a SoundPool and a SparseIntArray I am using sound files here, you will have to modify this part.
private static SoundPool soundPool;
private static SparseIntArray soundPoolMap;
public static final int S1 = R.raw.good_1, S2 = R.raw.bad_1,
P1 = R.raw.power_1,
P2 = R.raw.power_2,
P3 = R.raw.power_3,
P4 = R.raw.power_4,
P5 = R.raw.power_5,
WIN = R.raw.round_win;
Initialize your sounds and add them to the map
public static void initSounds(Context context)
{
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new SparseIntArray(8);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundPoolMap.put(S1, soundPool.load(context, R.raw.good_1, 1));
soundPoolMap.put(S2, soundPool.load(context, R.raw.bad_1, 2));
soundPoolMap.put(P1, soundPool.load(context, R.raw.power_1, 3));
soundPoolMap.put(P2, soundPool.load(context, R.raw.power_2, 4));
soundPoolMap.put(P3, soundPool.load(context, R.raw.power_3, 5));
soundPoolMap.put(P4, soundPool.load(context, R.raw.power_4, 6));
soundPoolMap.put(P5, soundPool.load(context, R.raw.power_5, 7));
soundPoolMap.put(WIN, soundPool.load(context, R.raw.round_win, 8));
}
Play the sound
public static void playSound(Context context, int soundID)
{
float volume = 1;
if(soundPool == null || soundPoolMap == null)
{
initSounds(context);
}
soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);
}
An example: playSound(this, P1);
Whats happening is I am using the SoundPool class and then mapping out audio streams with a SparseIntArray
精彩评论