0

I have method to generate tone, but I want play it only through ONE channel, left or right.

When I use tone.setStereoVolume(0,1) or track.setStereoVolume(0,1); it not work fine, because I hear sound through two channels.

Method disableSound disable sound on two channels.

I don't want to record any sound, just play it, so I chose AudioTrack.

Maybe has someone idea, how make it?

public void start(View view){
    view.setClickable(false);

    AudioTrack tone = generateTone(5000, 10);
    tone.play();

    view.setClickable(true);
}



private AudioTrack generateTone(double freqHz, int durationSec)
{


    int count = (int)(44100.0 * 2.0 * durationSec);
    short[] samples = new short[count];
    for(int i = 0; i < count; ++i){
        short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
        samples[i] = sample;
    }
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
            count * (Short.SIZE/8), AudioTrack.MODE_STATIC);
    track.write(samples, 0, count);

    return track;
}

public static void disableSound(Context context)
{
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, 0);

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anna J
  • 9
  • Not that it helps you, but note that method is [deprecated](https://developer.android.com/reference/android/media/AudioTrack.html#setStereoVolume(float,%20float)). – Andy Turner Oct 17 '17 at 17:51
  • Try `AudioFormat.CHANNEL_OUT_FRONT_LEFT` and `AudioFormat.CHANNEL_OUT_FRONT_RIGHT`. `setStereoVolume` is actually setting gain which may or may not explain why it doesn't work. – weston Oct 17 '17 at 17:55
  • I tried all _AudioFormat_ and it isn't work @EJK, thanks for this link, so my solution is to use SoundPool? – Anna J Oct 17 '17 at 18:02

0 Answers0