2014-08-30 320 views
0

我已经使用mediaplayer成功录制了用户的声音,并且该文件存储在SD卡中。 现在我想用音轨播放那个声音。但是当我这样做的时候会产生噪音。 是的。AudioTrack无法正常播放音频文件,因为它会产生噪音

这里是播放声音的代码..

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(); } 
+0

定义“制造噪音...” – 2014-08-30 23:39:43

+0

噪音你可以说它是在声音中产生很大的失真,它不是清晰地播放声音。 – AndroidiVore 2014-08-31 11:11:02

+0

@ Kenny正在等待 – AndroidiVore 2014-08-31 16:43:17

回答

0

噪声可能是由于一个小缓冲区。

尝试:

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

如果你完全最小缓冲区大小工作,它可能会导致在嘈杂的声音。最小尺寸的两倍是一个很好的做法(根据我的经验)。