有没有办法为Android手机编写android java代码,通过耳机插孔发送嗡嗡声(我想某个频率)并左右独立地进入耳塞。基本上我想给左耳发一个嗡嗡声。我想向正确的耳朵发出嗡嗡声。
答案 0 :(得分:1)
这是一个非常有趣的问题所以我虽然我试一试,我写了一个以所需频率生成音调然后播放它的课程,我无法测试左/右音量部分因为我不能找到我的耳机,但它应该工作!
希望这有帮助!
肯尼
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
public class ToneGen {
int seconds;
int sampleRate = 16000;
double frequency;
double RAD = 2.0 * Math.PI;
AudioTrack aTrack;
/**
* @param frequency The frequency of the tone
* @param duration The duration of the tone in seconds
* @param leftVolume Left volume 0.0f - silent, 1.0f full volume
* @param rightVolume Right volume 0.0f - silent, 1.0f full volume
*/
public ToneGen(double frequency, int duration, float leftVolume, float rightVolume){
this.frequency = frequency;
seconds = duration * 2;
byte[] buffer = new byte[sampleRate * seconds];
for ( int i=0; i<buffer.length; i++ )
{
buffer[i] = (byte)( Math.sin( RAD * frequency / sampleRate * i ) * 127.0 );
}
aTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
buffer.length, AudioTrack.MODE_STATIC);
aTrack.setStereoVolume(leftVolume, rightVolume);
aTrack.write(buffer, 0, buffer.length);
}
public void Play(){
aTrack.play();
}
public void Stop(){
aTrack.stop();
}
}