2014-08-30 63 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; 

如果你完全最小緩衝區大小工作,它可能會導致在嘈雜的聲音。最小尺寸的兩倍是一個很好的做法(根據我的經驗)。

相關問題