Sunday, October 23, 2011

stream audio via udp on android

In this post, I tried to play small audio data chunks with AudioTrack to show the feasibility of streaming audio. It's not straightforward enough. So I updated the sample code to actually transfer audio data with udp.
As the image below shows, the application is not too complicated.

The two buttons will each create a new thread to send and recv audio data respectively. And the sender will read audio data from the file /data/1.wav. Then, it sends the data to udp port 2048 on localhost. The receiver reads on 2048 port and feed data received to AudioTrack object.
Full code is available at:


Anonymous said...

thank you, that's exactly what I was looking for! :)

n0name said...

Thanks for good example!
Is it possible to stream audio over OutputStream(bluetooth transfer capability). I can't find correct way how realize it. If you know tell me please direction =)

nidz said...

hw do i get full duplex in this??

rxwen said...

To achieve full duplex, start receiver and sender threads on each peer simultaneously.

nidz said...

so instead f buttons n on click n dn calling - all i do is call both the func send and receive together rite??

nidz said...

in dis fr prog 2 run do v need 2 do port forwarding also?? or jus run d code on 2 wifi conn machines ??

rxwen said...

no need to set up port forwarding if these two devices are in the same lan.

nidz said...

my log cat shows byt_count but d receiving end aint playing d audio i hav sent from my sd card.. so isnt any ip addr required for directing d audio towards d odr end...

nidz said...

01-27 16:20:24.968: D/UdpStream(345): btnSend clicked
01-27 16:20:24.987: E/UdpStream(345): start send thread, thread id: 10
01-27 16:20:24.997: D/UdpStream(345): bytes_count : 800
01-27 16:20:25.073: D/UdpStream(345): bytes_count : 1600
01-27 16:20:25.143: D/UdpStream(345): bytes_count : 2400
01-27 16:20:25.213: D/UdpStream(345): bytes_count : 3200
01-27 16:20:25.287: D/UdpStream(345): bytes_count : 4000
01-27 16:20:25.367: D/UdpStream(345): bytes_count : 4800
01-27 16:20:25.440: D/UdpStream(345): bytes_count : 5600
01-27 16:20:25.517: D/UdpStream(345): bytes_count : 6400
01-27 16:20:25.588: D/UdpStream(345): bytes_count : 7200
01-27 16:20:25.658: D/UdpStream(345): bytes_count : 8000
01-27 16:20:25.728: D/UdpStream(345): bytes_count : 8800
01-27 16:20:25.799: D/UdpStream(345): bytes_count : 9600
01-27 16:20:25.877: D/UdpStream(345): bytes_count : 10400
dts my log cat in sending side n blank on receivers end!!

Anonymous said...

Could you please contact me on
I tried to contact you but couldn't.


I am looking for a player which can play UDP and RTSP stream on android 2.2 platform.

Could u please help me regarding this?

Plase respond positively, Crossing my fingers and hoping for the best...


hoang nguyen huy said...

I get a url multicast (udp, rtmp, mms..) (like IPTV)
Can I stream it on android ?
Please help.

hoang nguyen huy said...
This comment has been removed by the author.
Edwin said...

I have understood that the audio data is PCM. I can compress the audio before sending via UDP?.

rx wen said...

Yes, it's definitely possible. Just decode it at the receiving end before play a sample.

Anonymous said...

Thank you so much. This works for me locally. However, when I tested it over the internet (using external/public ip) I have to do port forwarding. I am about to distribute the apk for testing purposes and I don't want to require each of them to manually do port forwarding. How can this be done without port forwarding?

Anonymous said...

How can I download the streaming audio project folder and import to my android ADT? thanks!

setu basak said...

how can i send voice stream from one android device to multiple android device via server??can u plz give me any reference or link??

Sayuri said...
This comment has been removed by the author.
Anonymous said...

I am trying on same phone, it just giving me noise.
I mean started receiving thread and then I started streaming thread.
It receives packets but only noise is coming not actual audio.
Please help.

Quoc Duong Giang Hoang said...

I cant stop this thread and I dont know why.
When I send again, it overlap with the previous.
Can anyone solve my problem?
Thanks for your help

jordanstyle said...

cannot access code.. can you please send me the code thanks