AVAudioPlayer Volume Issue
01 May 2009, 16:00 Filed in: iPhone | Software Development
The AVAudioPlayer API that was introduced in the iPhone OS 2.2 is a very easy to use library for managing the playback of sound. Unfortunately, it has one significant flaw. The user can adjust the volume of an app using AVAudioPlayer for sound play back only while an AVAudioPlayer is active. That is, if the user presses the sound volume rocker on the side of his iPhone when the app is not actively playing a sound through AVAudioPlayer, the volume adjustment only applies to the phone's ringer level and not to the app's playback volume.
A simple work around for this limitation in AVAudioPlayer is to create a dummy AVAudioPlayer object at application launch (you will need a sound file/data to do this), and then call the prepareToPlay method on that dummy object. Retain the dummy object for the life of the app, releasing it upon app exit. By doing this, there will always be an active AVAudioPlayer object in the app, thus allowing the iPhone sound volume rocker to always affect the app's volume while it is running.
A simple work around for this limitation in AVAudioPlayer is to create a dummy AVAudioPlayer object at application launch (you will need a sound file/data to do this), and then call the prepareToPlay method on that dummy object. Retain the dummy object for the life of the app, releasing it upon app exit. By doing this, there will always be an active AVAudioPlayer object in the app, thus allowing the iPhone sound volume rocker to always affect the app's volume while it is running.
blog comments powered by Disqus