Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop HTML5 audio when iOS4 iPhone Web app is put into background?

I'm making a Web app (at chirpid.com) for the iPhone that plays audio files for cricket chirp identification. The user can start and stop the audio by tapping screen buttons. But if the user taps the home button while a sound file is playing, it continues to play in the background (an iOS4 feature). I want to stop the audio in this case. Is there an event or property that I can use via Javascript in Safari to determine when I have been put into the background?

like image 845
Bruce Martin Avatar asked Jul 27 '10 19:07

Bruce Martin


1 Answers

You could manually trigger the "stop" button like this when closing the application.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if(audioIsPlaying) {
        [btnStop sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}

You get 5 seconds to do anything when using the applicationDidEnterBackground: function. That should be enough to stop the audio ;-).

like image 145
Leon Avatar answered Oct 21 '22 17:10

Leon