一些特殊的情况迫使我做出了那种变态的事情。
是否可以在不同通道上播放 2 个不同的音频流。想象一下耳机,我需要同时在左扬声器上播放第一首歌曲,在右扬声器上同时播放第二首歌曲。
经过一些研究,我发现可以在某个单一频道上播放。甚至可以关闭其中之一。但我没有找到任何如何同时播放 2 个音频流的信息。
有可能吗?如何?一些代码示例和链接表示赞赏!
研究成果。
1)AudioTrack
能够在不同的频道上播放
// 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
可以分别设置左右声道的音量。因此从技术上讲,可以启动 2 个流,并将左音量设置为 0,右音量设置为 100,第二个流则相反。正确的?
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)
也许有这样的解决方案使用MediaPlayer
这样就更好了!
SOLUTION:
似乎最简单的解决方案是使用SoundPool
。我测试的方式。
//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();
}
}
虽然缺乏像这样的功能MediaPlayer.OnCompletionListener()
。现在确定如何实现这一点,但无论如何..问题解决了。