我有生成音调的方法,但我想通过一个频道向左或向右播放。
当我使用tone.setStereoVolume(0,1)或track.setStereoVolume(0,1)时;它不能正常工作,因为我通过两个声道听到声音。
方法disableSound禁用两个通道上的声音。
我不想录制任何声音,只是播放它,所以我选择了AudioTrack。
也许有人有想法,怎么做?
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);
}