AudioTrack无法正常播放音频文件,因为它会产生噪音 [英] AudioTrack is not playing audio file properly as it is making noise instead
本文介绍了AudioTrack无法正常播放音频文件,因为它会产生噪音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用mediaplayer成功录制了用户的语音,并且文件存储在SD卡中.现在我想使用音轨播放该声音.但是,当我这样做时,它正在发出声音.是吗?
I have recorded user's voice successfully using mediaplayer and the file is stored in sd card. now i want to play that voice using audio track. But when I do it is making noise. Y so.?
这是播放声音的代码.
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(); }
推荐答案
噪声可能是由于缓冲区较小所致.
Noise could be due to a small buffer.
尝试:
int intSize = android.media.AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
init *= 2;
如果正好使用最小的缓冲区大小,则可能会产生嘈杂的声音.根据我的经验,最小尺寸的两倍是一个好习惯.
If you work with exactly the minimum buffer size, it might result in noisy sound. Having twice the minimum size is a good practice (in my experience).
这篇关于AudioTrack无法正常播放音频文件,因为它会产生噪音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文