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:
http://code.google.com/p/rxwen-blog-stuff/source/browse/trunk/android/streaming_audio/src/rmd/media/StreamingAudio/UdpStream.java

9 comments:

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 =)
Regards..

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