Android - Multiple, simultaneous streaming audio

Currently, I need to implement an application on an Android device that can open streams of audio in real time and play them back to the user. This is not a big deal, and I successfully set up a streaming server and earned it for a single file. However, my question is, how can I play and play multiple audio files at the same time?

When I open several streams (using MediaPlayer) and prepare and play each file, I hear only sound from one file. I'm not sure this is possible at the moment, but I hope so.

I studied the use of SoundPool, but it seems that this is only for local media, and I MUST have this streaming transmission since it is important for plaback speed (no latency, etc.).

Any help or understanding is greatly appreciated! Thanks in advance!

+6
source share
1 answer

I used to work on the application, and I had a similar problem, and I was able to fix it, except that I did not broadcast audio. Hope this helps, you will need to change it for your purposes:

import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.util.SparseIntArray; 

Create sound effects and SparseIntArray Here I use sound files, you will have to change 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 card

 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 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); } 

Example: playSound (this, P1);

What happens, I use the SoundPool class and then display the audio streams using SparseIntArray

+1
source

Source: https://habr.com/ru/post/885770/


All Articles