0

I am recording user's voice through media player and saved it in .m4a format. Recording is successfully file saved successfully now i want to play that audio file through audioTrack with a different pitch. but my code is not playing the audio file properly instead it is giving noise sound. Why is it so.?

Here is my code :

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

Thanx in advance..

Workaholic
  • 93
  • 12
  • `AudioTrack` only plays uncompressed PCM. Your m4a file probably contains audio compressed with AAC or some other encoder. You'll have to implement the decoding yourself, or with the help of some 3rd party library, if you want to use `AudioTrack`. – Michael Sep 01 '14 at 13:57
  • @Michael thanx for reply.. but how can i decode that OR which library i should use for that..? OR can i change pitch of sound using mediaplayer.? – Workaholic Sep 01 '14 at 14:56

0 Answers0