Play sound in left or right speaker using android AudioTrack

前端 未结 1 690
别跟我提以往
别跟我提以往 2020-12-20 07:36

I am playing sound with the help of AudioTrack in my APP but i want to play sound in specific speaker/ear means left speaker or right speaker or both speaker.

Follow

相关标签:
1条回答
  • 2020-12-20 08:03

    AudioTrack uses raw PCM samples to play sounds. The PCM samples are played in the following order (the first sample is played by the left speaker and the second sample is played by the right speaker):

    LRLRLRLRLR

    So you have to modify your samples array that you pass to AudioTrack.

    This could help too.

    In your case just do the following:

    // 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;
    }
    
    0 讨论(0)
提交回复
热议问题