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

Advertisement

4 Responses to “stream audio via udp on android”

  1. Ayn Says:

    Hi Rxwen,
    thanks for the post…it helped me to a gr8 extent in understanding udp streaming in android.
    I want to build some extra stuff using the same code. Could you please help me with following:
    1.How do I connect multiple devices for udp streaming sothat when I press Send button it can be received by say 3-4 phones in same network.

    2. Is it possible to send larger size audio files like mp3 using the same code?

    3. What do I need to add in the code so that I can add more controls like send next file or previous file?

    Thx
    Ayn

  2. rxwen Says:

    1.How do I connect multiple devices for udp streaming sothat when I press Send button it can be received by say 3-4 phones in same network.

    You send audio data to a multicast address, and all other phones join the same multicast group should be able to receive audio data.

    2. Is it possible to send larger size audio files like mp3 using the same code?

    You can send mp3 with similar code, but be noted that AudioTrack can’t decode mp3 itself. You have to decode the mp3 to PCM yourself before feed to AudioTrack.

    3. What do I need to add in the code so that I can add more controls like send next file or previous file?

    The basic idea is to add a Next/Previous button, and in click event handler of these buttons, first stop the sender thread, then start a new sender thread. In the new sender thread, you can read a different file and send.

    • Ayn Says:

      Thanks that helps :) .. will work more on it and let you know how it went..
      Beside one small question: How do I make udp streaming work over 3g network ? How do i specify the Inet addresses in that case ?

      • rxwen Says:

        I didn’t actually work on a 3g network.
        Based on my understanding, it’s irrelevant whether you are using wifi or 3g. From application level’s point of view, no matter what the physical connection is, there is a working tcp/ip stack.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.