audioTrack发出嘈杂的声音嘈杂意味着会产生很大的失真

时间:2014-09-01 11:02:03

标签: android audio android-mediaplayer audiotrack

我正在通过媒体播放器录制用户的语音并以.m4a格式保存。录制成功保存成功文件现在我想通过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 个答案:

没有答案