How to stop HTML5 audio when iOS4 iPhone Web app is put into background?
I'm making a Web app (开发者_如何学Pythonat 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?
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 ;-).
I would guess you need to create a listener that listens to system events...
Perhaps you should read this book: http://books.google.com/books?id=a09NMFdA6m0C&pg=PA94&lpg=PA94&dq=iOS+home+button+event+javascript&source=bl&ots=4Nop45gmwI&sig=8mTEGQI1ym65H7XjmBSzRQ2KyMc&hl=en&ei=_qDWTrmMDsPh4QSR9I2rAQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CEsQ6AEwBQ#v=onepage&q=iOS%20home%20button%20event%20javascript&f=false
iOS4 has some kind of lock function you can assign to buttons, perhaps you could tie in a script to turn down volume that way.
If you found the solution be nice and post it here for others to see.
Best of luck!
精彩评论