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

21 comments:

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

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

    ReplyDelete
  3. hw do i get full duplex in this??

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

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

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

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

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

    ReplyDelete
  9. 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!!

    ReplyDelete
  10. Could you please contact me on outtrylook@gmail.com
    I tried to contact you but couldn't.

    Anyway,


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

    Thanks

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

    ReplyDelete
  12. This comment has been removed by the author.

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

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

    ReplyDelete
  15. 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?

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

    ReplyDelete
  17. 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??

    ReplyDelete
  18. This comment has been removed by the author.

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

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

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

    ReplyDelete