Some special circumstances force me to do that perverted thing.
Is it possible to play 2 different audio streams on different channels. Imagine a headset and I need to play 1st song at left speaker and 2nd song on right speaker simultaneously.
After some research I found that it is possible to play at some single channel. Its possible even to close one of them. But I didn't find any information how to play 2 audio streams simultaneously.
Is it even possible? And how? Some code examples and links appreciated!
Research results.
1)AudioTrack
able to play on different channels
// only play sound on left
for(int i = 0; i < count; i += 2){
short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
samples[i + 0] = sample;
samples[i + 1] = 0;
}
// only play sound on right
for(int i = 0; i < count; i += 2){
short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
samples[i + 0] = 0;
samples[i + 1] = sample;
}
2) SoundPool
able to set volume for left and right channels separately. So technically it's possible to start 2 streams and set for one 0 left volume and 100 for right volume and vice versa for 2nd stream. Right?
setVolume(int streamID, float leftVolume, float rightVolume)
sample:
SoundPool pool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
pool.play(
pool.load(this, R.raw.my_sound_effect, 1), // The sound effect to play
0, // The volume for the left channel (0.0 - 1.0)
1.0f, // The volume for the right channel (0.0 - 1.0)
0, // Default priority
0, // Do not loop
1.0f); // Playback rate (1.0f == normal)
Perhabs there's such solution using MediaPlayer
so that would be much preferable!
SOLUTION:
Seems like the easiest solution is to use SoundPool
. The way I tested it.
//SDK Version
public CustomSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
} else {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
}
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1.0f, 0, 0, 0, 1.0f); //left channel
//soundPool.play(sampleId, 0, 1.0f, 0, 0, 1.0f); //right channel
}
});
}
public void playSound(String path) {
if (soundPool != null) {
soundPool.load(path, 1);
}
}
public void release(){
if (soundPool != null) {
soundPool.release();
}
}
Although there's lack of features like MediaPlayer.OnCompletionListener()
. Now sure how to implement this but anyway.. Problem solved.