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.
Following code i am using to play sound.
private AudioTrack generateTone(double freqHz, int durationMs)
{
int count = (int)(44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
short[] samples = new short[count];
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] = 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;
}
With following code i am calling this function to play sound.
AudioTrack soundAtSpecificFrequency = generateTone(500, 6000);
soundAtSpecificFrequency.play();
Following code to stop playing sound.
soundAtSpecificFrequency.pause();
Can you please tell me what can the possible solution in this case or any other alternative solution?
Thanks for your precious time.
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: