Live and Progressive Streaming with Android


You may have seen my prior post on this topic: Streaming Media with Android.  The title is slightly innacurate as it’s less about generic ‘media’ streaming and instead focused on .mp3 audio.  The basic streaming function described in the post is still valid for Android however that post was written when Android was still v1.0.  Android’s media handling has improved substantially since then but is still unstable with respect to many media formats.  In my experience, it’s best to stick to .mp3 for audio and .3gp for video.  The best tool I’ve found for generating .3gp video is Quicktime Pro.  Yes…oddly Apple makes the easiest tool for generating video for Android.

Scroll to the bottom to see a few tutorials I’ve found on Live Streaming with Android.

Streaming Media…As In Live Radio

My prior streaming media post has recieved many questions about streaming live audio/video.  That type of streaming is completely different from what I previously discussed, but since there’s so much interest, I though I would write a brief post that I can refer people to.  To start, here’s the list of Android’s supported media formats.  As you can see, Android now supports streaming of these live media formats:

  • HTTP/HTTPS progressive streaming
  • HTTP/HTTPS live streaming draft protocol:
    • MPEG-2 TS media files only
    • Protocol version 3 (Android 4.0 and above)
    • Protocol version 2 (Android 3.x)
    • Not supported before Android 3.0
    • Note: HTTPS is not supported before Android 3.1.

Progressive versus Live Streaming

This distinction is important and may or may not be under your control.  If you are streaming your own media, then research the strengths and weaknesses of these two options to determine what’s right for you.  If your app will play somebody elses content, then you’ll need to determine which format they are using.  In general though, if the media to stream is self-contained in a file, then progressive streaming is the easiest solution.  If the media is of indeterminate length because it’s being generated or created live (e.g. radio or live video blog), then you’ll need ‘live’ streaming.

Progressive Streaming

File delivery via HTTP is referred to as ‘progressive or http streaming’.  This isn’t really file streaming as much as complete download of a media file to the user’s computer but with playback initiated as soon as sufficient content has been downloaded. The media file is buffered onto the user’s computer so it can be viewed/listened to. In technical terms, the initially downloaded file bytes must contain a header describing the media file format, encoding quality (e.g. 128 kbps) and such.  The server doesn’t care about the file’s content though, it simply sends data to the app as fast as it can.  Because the file is downloaded from in its entirety, the ability to skipping to parts of the file that have not yet been downloaded is not possible…the user must wait for the server to stream all the prior content before playing the selected section.

Code: Progressing Streaming

Go here for the code from my progressive streaming post.

Live Streaming

Live streaming requires the overhead of a streaming server – special software that handles the media requests.  This is different from progressive streaming in which a standard web server can simply deliver a media file via HTTP.  Live streaming requires a more intelligent conversation between the streaming server and the local app performing media playback.  The user’s app must provide ‘control handling’ between itself and the server. These control messages include ‘play’, ‘pause’, and ‘seek’ but also details about the quality of the media to be delivered.  E.g. if the app is on a slower network, then it should request lower quality (i.e. kbps) media and if the device has a smaller screen, it would request video for that size to reduce unnecessary bandwidth usage.

The advantages of Live Streaming are the ability to begin playback at any point, skip through the media, more efficiently use bandwidth, and to avoid storing media file on the device (the data is played and discarded immediatley – e.g. the difference between Apple’s 1st and 2nd generation Apple TV).

When selecting your streaming servers, you’ll need to determine  the appropriate streaming protocols: RTSP(Real time streaming protocol), RTMP(Real time messaging protocol) and MMS (Microsoft media services). These streaming protocols deliver video well by being more focussed on continuous delivery than 100% accuracy. The assumption is that it’s better to have a momentary glitch rather than stopping media playback altogether.

Code & Tutorials: Live Streaming

CatDaaaady repurposed my progressive streaming code and wrote code to listen to NPR’s streaming broadcast.  In his words, “I used Biosopher’s code and modified it to support a continuous stream of audio instead of a file. It downloads X number of seconds and save that audio segment. Then it starts playing that audio clip while it downloads the next sections of audio in the background. Then queuing up the audio segments for playing next. It is not perfect though. :-( I get a slight playback pause between audio segments though. Hopefully I will prefect it or find another way around.”

And here are a couple posts from other people w/code samples for Live Streaming on Android.

Android Map Tutorials Updated to v1.5 (Cupcake) – MapView & MapActivity

I updated the map tutorials to v1.5 as well.  There were enough tricky challenges to getting maps working on Android that I thought it best to pass the information along. Here are the source files for this tutorial as well as all my other tutorials.

Tutorial 1: Transparent Panel (Linear Layout) On MapView (Google Map)
Tutorial 2: “Hit” testing on a View (MapView)

The first challenge to using Google’s Maps in Android is simply informing Eclipse that you’ll need access to Google’s mapping API.  Instead of writing my own tutorial on this, I’ll simply point you to the same well-documented Android v1.5 MapActivity setup that I followed.

The second challenge as always is getting your Map API key to function properly. Google provides full details on installing your Google Map key.  I spent several hours trying to figure out why my API key wasn’t working though…my Google maps always displayed as empty white/gray tiles.  Eventually I tracked the problem down to the fact that Android had created multiple debug.keystore files on my computer.  I’m running Vista so my .keystores were located in this directory:

Correct: C:\users\<username>\.android\debug.keystore

I wasted time by using the .keystore in my AppData folder as this was the original place where the .keystores were stored in Android v1.0.  However it now seems that Android now uses the .android directory instead.  If you have problems, figure out which directory Eclipse is pulling your .keystore from when signing your apps.

Android Streaming MediaPlayer Tutorial – Updated to v1.5 (Cupcake)

NOTE: This tutorial is about progressive audio.  If you want to stream live media, then please read my post on live and progressive streaming with Android.

After spending the last year game programming on the iPhone, I’ve finally returned to Android.  My six prior tutorials were outdated (Android v1.0) so I took the time to update them to v1.5 (Cupcake).  The most popular of those tutorials was the Streaming MediaPlayer tutorial so that’s the primary focus of this post.

Tutorial #3 results screenshots

At the time of initially writing the streaming tutorial, Android’s media streaming function didn’t work well so I wrote my own.  As of v1.5 however, Android’s MediaPlayer streams very well.  That said, it’s still useful to know how to retrieve a media file from a server and store it on the device.  This would be useful to immediate replay of the file at a later date or for caching files for later playback ‘off the grid’.

Download the source files to get started immediately and then view the rest of the tutorial after the jump.

Continue reading ‘Android Streaming MediaPlayer Tutorial – Updated to v1.5 (Cupcake)’

Android Challenge 1 Round 2

First we want to thank everyone for their comments about our prior Android tutorials.  We’re very glad to have helped you guys.  Many of you have asked why we haven’t posted new tutorials, and the reason is that happily, we’ve spent the few months working hard on our submission into Round 2 of Android Challenge #1.  Yes…if you hadn’t heard already, we were one of the top 50 finalists.

As soon as Google releases the next SDK, we’ll write up more tutorials based on the lessons we’ve learned over the past months.

Until then, we’re getting out to exercise and enjoy some sunshine away from our desks.


Anthony (Biosopher)

Android Application Lifecycle Demo

An excellent video on Android’s application lifecycle has been posted today by Sleepy Droid:

Demo video here

I thought I already knew everything about Android’s app lifecycle, but this visual refresher and sample application were a great refresher. Understanding the application lifecycle is crucial to writing good Android apps so this video shoud be useful to pros and newbies alike.

Sleepy Droid says they’ll be creating more videos if the response is positive so visit Sleepy Droid to see if new videos have been posted.

Thanks Sleepy Droid!

iPhone Getting GPS & SDK Getting Maps API…?

Since I dissed the iPhone SDK earlier this month (iPhone SDK lacks mapping function), I wanted to pass on this update. Looks like a GPS-enabled iPhone is in the works for Apple’s upcoming World Wide Dev Conference. Even if the announcement doesn’t come then, looks like it will come soon according to EnGadget:

Second-gen iPhone: 3G, GPS, only slightly thicker

Click to see new GPS-iPhone at EndGadget

This is great news for mobile map-based application developers but don’t count on the API being as slick and open as Android’s. Given Apple’s approach so far, they’ll still withhold the vital functionality to get your app really humming.

Why do I say that?

Well for example, Apple’s SDK doesn’t allow applications to run in the background. That means death to any Pocket Journey app that would run on an iPhone as the audio/video would be killed along with your application as soon as someone called. Apple says this is for security reasons, but we know it’s due to Apple’s “control freakish” corporate nature. Happily for us, Android solved this challenge through their Service API.

Seems like Apple wants to step back to the dark ages of the Palm OS which can only handle single-threaded tasks as well

Android Tutorial #4.2: Passing custom attributes via XML resource files

In tutorial #4.1, I mentioned that we passed custom attributes for the text and image variables from the XML resource file to our custom class. This is a critical skill for performing true object-oriented programming and how to do it wasn’t obvious from Google’s Android API Demos.

Luckily I was pointed to the solution myself by an experienced Android programmer in Guatemala by the username of cadlg (thanks again!). If you want to see the official Google Android example though, look at Android’s APIDemos’ custom LabelView example.

So here we go. We’ll use the same code as Tutorial 4.1 to keep this simple.

Continue reading ‘Android Tutorial #4.2: Passing custom attributes via XML resource files’