Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play PCM stream in Android

I am getting PCM streams through ethernet port. So far, I am able to capture the packets and takeout the pcm_payload data from them.

How to play this raw PCM data in android? The PCM data is 16-bit 2 channel, 44.1kHZ rate stream.

I am both new to android application programming and audio programming. Sorry if this is a trivial question.

like image 299
Sandeep Avatar asked Feb 05 '23 09:02

Sandeep


1 Answers

You can use AudioTrack to play PCM data!

Maybe like this:

int bufsize = AudioTrack.getMinBufferSize(44100,
           AudioFormat.CHANNEL_OUT_STEREO,
           AudioFormat.ENCODING_PCM_16BIT);

AudioTrack audio = new AudioTrack(AudioManager.STREAM_MUSIC, 
                       44100, //sample rate
                       AudioFormat.CHANNEL_OUT_STEREO, //2 channel
                       AudioFormat.ENCODING_PCM_16BIT, // 16-bit
                       bufsize, 
                       AudioTrack.MODE_STREAM );
audio.play()

then invoke audio.write() to write your PCM data.

like image 89
木大白易 Avatar answered Feb 09 '23 02:02

木大白易