PCM Raw Bytes [] To Audio on Android

26,706

Solution 1

http://developer.android.com/reference/android/media/AudioTrack.html

This class looks like it has exactly what you need!

write(byte[] audioData, int offsetInBytes, int sizeInBytes)

Writes the audio data to the audio hardware for playback.

EDIT

Change your stream to AudioManager.STREAM_MUSIC

Solution 2

The question is whether you want to play PCM samples you receive from some source directly in the Android app or if you have some external files you need to convert. In the first case go the way as jack57 described. It is really easy and you can even add markers to have precise timing.

int bufferSize = AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);

mAudioTrack.play();

then call write method in a cycle:

mAudioTrack.write(buffer, 0, buffer.length);

In the second case just use some free audio editor: http://audacity.sourceforge.net/download/. You can store PCM samples as a .txt file, open it in the editor and save in a format you need...

Share:
26,706
ShannonS
Author by

ShannonS

Updated on July 09, 2022

Comments

  • ShannonS
    ShannonS almost 2 years

    I currently have a PCM audio in the form of a byte array. The format is signed 16 bit little endian. I would like to convert this to some playable format on the Android, preferably version 3.2 or higher. Does anyone have suggestions on how this can be done? I have done some research and tried the following below, but none were successful. It would be much appreciated if anyone can suggest a better way or indicate where I have gone wrong.

    I have tried creating an AudioFormat with the correct audio settings, however Android does not support the javax.sound.sampled library.

    I have also tried writing the PCM data into a wave file, but I am running into trouble getting all of the necessary wav header information.

    I have also tried using the AudioTrack class offered by the Android, to play the PCM data. However, I can't seem to hear any sound. Here is my method:

        public void playback(){
             try{
                  AudioTrack audioTrack = new  AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, 500000, AudioTrack.MODE_STATIC);
                  audioTrack.write(audio, 0, 500000);
    
          } catch(Throwable t){
            Log.d("Audio","Playback Failed");
        }
    }
    

    Any suggestions would be appreciated.

  • ShannonS
    ShannonS almost 12 years
    Thank you for directing me to fix the stream. This contributed to fixing the error.
  • Jack Satriano
    Jack Satriano almost 12 years
    Please select a correct answer, it will help you receive more answers in the future.
  • Feroz Siddiqui
    Feroz Siddiqui over 4 years
    can we write this in a file and store it ?
  • Feroz Siddiqui
    Feroz Siddiqui over 4 years
    how can this write it into a file ?