In the last chapter, we explored Android's basic audio playback capabilities. While those capabilities are fantastic, we need to push a bit further to make them generally useful. In this chapter, we'll look at how we can do things like play audio files in the background so that the application playing the audio doesn't need to be running. We'll take a look at how we can synthesize sound rather than just playing sound files, and we'll look at how to leverage streaming audio that is available on the Internet.
So far we have concentrated on building applications that are centered around being in the foreground and have their user interface in front of the user. In the last chapter, we looked at how to add audio playback capabilities to those types of applications.
What happens, though, if we want to build an application that plays music or audio books, but we would like the user to be able to do other things with the phone while continuing to listen? We might have some trouble making that happen if we limit ourselves to just building activities. The Android operating system reserves the right to kill activities that aren't in the front and in use by the user. It does this in order to free up memory to make room for other applications to run. If the OS kills an activity that is playing audio, this would stop the audio from playing, making the user experience not so great.
Fortunately, there is a solution. Instead of playing our audio in an activity, we can use a Service.
In order to ensure that the audio continues to play when the application is no longer in the front and its activity is not in use, we need to create a Service. A Service is a
Was this article helpful?