采用了android AudioTrack播放声音在左或右扬声器 [英] Play sound in left or right speaker using android AudioTrack

查看:836
本文介绍了采用了android AudioTrack播放声音在左或右扬声器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩声音AudioTrack在我的应用程序的帮助,但我想在特定的扬声器/见机行事声音指左扬声器或右扬声器或两个扬声器。

I am playing sound with the help of AudioTrack in my APP but i want to play sound in specific speaker/ear means left speaker or right speaker or both speaker.

继code我用来播放声音。

Following code i am using to play sound.

private AudioTrack generateTone(double freqHz, int durationMs)
{
    int count = (int)(44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
    short[] samples = new short[count];
    for(int i = 0; i < count; i += 2){
        short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
        samples[i + 0] = sample;
        samples[i + 1] = 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;
}

通过以​​下code我打电话这个功能来播放声音。

With following code i am calling this function to play sound.

AudioTrack soundAtSpecificFrequency =   generateTone(500, 6000);
soundAtSpecificFrequency.play();

继code停止播放声音。

Following code to stop playing sound.

soundAtSpecificFrequency.pause();

能否请你告诉我什么可以在这种情况下,或任何其他替代解决方案的可能的解决方案?

Can you please tell me what can the possible solution in this case or any other alternative solution?

感谢您的precious时间。

Thanks for your precious time.

推荐答案

AudioTrack 使用原始PCM样本播放声音。在PCM样品按以下顺序播放(第一样品由左扬声器播放与所述第二样品是由右扬声器播放):

AudioTrack uses raw PCM samples to play sounds. The PCM samples are played in the following order (the first sample is played by the left speaker and the second sample is played by the right speaker):

LRLRLRLRLR

LRLRLRLRLR

所以,你必须修改你的样品阵列传递给 AudioTrack

So you have to modify your samples array that you pass to AudioTrack.

<一个href=\"http://stackoverflow.com/questions/20461243/how-to-play-two-sine-wave-on-left-and-right-channel-separately-with-16-bit-forma\">This可以帮助了。

在您的情况下,只需做到以下几点:

In your case just do the following:

// only play sound on left
for(int i = 0; i < count; i += 2){
    short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
    samples[i + 0] = sample;
    samples[i + 1] = 0;
}
// only play sound on right
for(int i = 0; i < count; i += 2){
    short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
    samples[i + 0] = 0;
    samples[i + 1] = sample;
}

这篇关于采用了android AudioTrack播放声音在左或右扬声器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆