发送嗡嗡声耳塞向左或向右耳塞、嗡嗡声

2023-09-06 18:55:45 作者:忆七.

有没有写的Andr​​oid的Java code,对于Android手机,通过耳耳机插孔和成左耳塞,右各自独立的发送嗡嗡的声音(我想一定频率)的方式另一个。基本上我想一个时髦发送到左耳塞。我想一个时髦发送给正确的耳塞。

Is there a way to write android java code, for an android phone, to send a buzz sound (i'm thinking a certain frequency) through the ear-phone jack and into earbuds left and right independently of one another. Basically I want to send a buzz to the left ear-bud. And i want to send a buzz to the right ear-bud.

推荐答案

这是一个很有趣的问题,所以我虽然我给它一去,我已经写在要求的频率播放,然后生成一个音类它,我不能测试左/右音量的部分,因为我无法找到我的耳机,但它应该工作!

This was quite an interesting question so i though i'd give it a go, i've written a class that generates a tone at a required frequency then plays it, i couldnt test the left/right volume part because i cant find my headphones but it should work!

希望这有助于!

肯尼

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();
    }
}
 
精彩推荐
图片推荐