gpt4 book ai didi

android - AudioTrack 没有正确播放音频文件,因为它发出噪音

转载 作者:行者123 更新时间:2023-11-30 02:42:27 28 4
gpt4 key购买 nike

我已经使用媒体播放器成功录制了用户的声音,文件存储在 sd 卡中。现在我想用音轨播放那个声音。但是当我这样做时,它会发出噪音。是吗?

这是播放声音的代码..

private void PlayAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;

int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);


if (at==null){
Log.d("TCAudio", "audio track is not initialised ");
return;
}

int count = 512 * 1024; // 512 kb
//Reading 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();
at.play();
while (bytesread < size) { ret = in.read( byteData,0, count); if (ret != -1) { // Write the byte array to the track

at.write(byteData,0, ret); bytesread += ret; } else break; } in.close(); at.stop(); at.release(); }

最佳答案

噪音可能是由于小缓冲区造成的。

尝试:

int intSize = android.media.AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
init *= 2;

如果您恰好使用最小缓冲区大小,则可能会产生嘈杂的声音。最小大小的两倍是一个很好的做法(以我的经验)。

关于android - AudioTrack 没有正确播放音频文件,因为它发出噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25586921/

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