gpt4 book ai didi

Android AudioTrack 在声音开始和结束时点击

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:05 28 4
gpt4 key购买 nike

我在播放声音(来自 SD 卡的 wav)的开始和结束时听到咔嗒声。这一定与轨道缓冲有关,但我不知道解决方案。另外,每次播放声音时我都会创建一个新的,这样可以吗还是有更好的方法?有许多声音一遍又一遍地播放。这是代码:

public void PlayAudioTrack(final String filePath, final Float f) throws IOException
{

new Thread(new Runnable() { public void run() {
//play sound here
int minSize = AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT );
AudioTrack track = new AudioTrack( AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,
minSize, AudioTrack.MODE_STREAM);

track.setPlaybackRate((int) (44100*f));

if (filePath==null)
return;

int count = 512 * 1024;
//Read the file..
byte[] byteData = null;
File file = null;
file = new File(filePath);

byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream( file );

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int bytesread = 0, ret = 0;
int size = (int) file.length();

while (bytesread < size) {
try {
ret = in.read( byteData,0, count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
track.play();
if (ret != -1) {
// Write the byte array to the track
track.write(byteData,0, ret); bytesread += ret;
}
else break; }

try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} track.stop(); track.release();
}

}).start();
}

非常感谢您的帮助

最佳答案

你不也可以播放PCM波文件头吗?

每个 PCM 波形文件在文件开头都有一个小标题,如果您播放它,您会播放标题字节,这可能会导致在开头出现咔嗒声。

关于Android AudioTrack 在声音开始和结束时点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203671/

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