gpt4 book ai didi

java - 流模式下的 AudioTrack MODE_STREAMING

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:56 27 4
gpt4 key购买 nike

我需要流式传输运行时生成的 PCM 数据。所以我有一个带循环的线程

public void run() {
while(...) {
mAudioTrack.write(getPCM(), ...);
}
}

不幸的是,这不起作用。它似乎不依赖于 AudioTrack 缓冲区大小。我希望它非常小以模拟某种低延迟行为(150 毫秒),以便用户可以动态更改 getPCM() 选取的 PCM

int bufferSize = 0.150 * sampleRate * channels * bitsPerSample / 8;

但是,我尝试将缓冲区大小增加到 100k,但没有结果

最佳答案

这是对我有用的简短示例:

public class Internal extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onPlayClicked(View v)
{
start();
}

public void onStopClicked(View v)
{
stop();
}

boolean m_stop = false;
AudioTrack m_audioTrack;
Thread m_noiseThread;

Runnable m_noiseGenerator = new Runnable()
{
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

/* 8000 bytes per second, 1000 bytes = 125 ms */
byte [] noiseData = new byte[1000];
Random rnd = new Random();

while(!m_stop)
{
rnd.nextBytes(noiseData);
m_audioTrack.write(noiseData, 0, noiseData.length);
}
}
};

void start()
{
m_stop = false;

/* 8000 bytes per second*/
m_audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT, 8000 /* 1 second buffer */,
AudioTrack.MODE_STREAM);

m_audioTrack.play();


m_noiseThread = new Thread(m_noiseGenerator);
m_noiseThread.start();
}

void stop()
{
m_stop = true;
m_audioTrack.stop();
}
}

关于java - 流模式下的 AudioTrack MODE_STREAMING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6179392/

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