gpt4 book ai didi

android - 向左耳塞或右耳塞发送嗡嗡声

转载 作者:行者123 更新时间:2023-11-30 04:38:15 25 4
gpt4 key购买 nike

有没有一种方法可以为 android 手机编写 android java 代码,通过耳机插孔发送嗡嗡声(我认为是特定频率)并独立于左右耳塞。基本上我想向左耳塞发送一个嗡嗡声。我想向右耳塞发送一个嗡嗡声。

最佳答案

这是一个非常有趣的问题,所以虽然我会试一试,但我编写了一个类,它以所需的频率生成音调然后播放它,我无法测试左/右音量部分,因为我不能找到我的耳机,但它应该可以用!

希望对您有所帮助!

肯尼

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

关于android - 向左耳塞或右耳塞发送嗡嗡声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com