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