I have been using Udacity for the past six months now, and for those who have never heard of them, I encourage you to check them out. Udacity has a number of excellent courses on a broad range of topics, and the programs built by Google are particularly well done. The ability to interact with the content through quizzes and activities is much more engaging than a traditional YouTube video, and there are forums for students who want to talk about the subject at hand with peers. I am currently working through the second level Android developer course, and I wanted to take a break and write about audio in mobile applications.
While to some it may seem mundane, a LOT of care and attention goes into adding audio to an application, and while many of these practices are not exactly necessary for an application’s function, providing a seamless and cohesive experience while being a “good mobile citizen” is a marker of a well designed piece of software.
For the full list of Android AudioManager constants, click here.
If we have continuous audio playing in our app, whether that be the main function, like in a music player app, or a side feature, say a background song in a game, we can be very unkind to our user and their device if we aren’t careful. You might not have noticed, but when you’re listening to a song on Spotify or Soundcloud, and someone texts you, the volume will briefly lower, allowing you to hear the notification.
This can be called with the constant:
This allows or application to release audio focus for a brief time, and allows the user to feel connected and informed. If we had not accounted for such an occurrence, the user would exit our application and see a missed text, call or other notification. I don’t know about you, but I wouldn’t feel particularly kindly towards any app that behaved like this.
Another way we can be good Android citizens is by releasing memory allocated for our application as soon as we are finished with it. If you are not familiar with application lifecycles, I highly suggest reading this documentation:
Whenever an audio file has finished playing, or if another non-concurrent file is started, that memory should be released back to the device. There are a number of other ways to tune audio playback in Android applications, and if you have any specific questions about best practices or how to add different features to your application, feel free to ask me!