PCM Raw Bytes [] To Audio on Android

后端 未结 2 1617
[愿得一人]
[愿得一人] 2020-12-24 13:50

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, pre

相关标签:
2条回答
  • 2020-12-24 14:28

    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

    0 讨论(0)
  • 2020-12-24 14:30

    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...

    0 讨论(0)
提交回复
热议问题