有没有一种方法可以为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();
    }
}

10-08 02:06