Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue playing audio from html5 player when in iOS background mode

We built our music-oriented app in html5 and javascript with Sencha touch. For distribution we wrapped it in xcode with UIwebView. Everything runs fine except one thing that does not work: audio playing in multitask mode.

I know the general idea: add the UIBackgroundModes in info.plist.

Done. Now we can play the audio even in background mode.

Until we reach the end of the song. To start the next song we have to bring the app to foreground again or we can hit the play or 'next song' button on the iPhone audio controller.

After some research I found a promising workaround at: " Entering background on iOS4 to play audio " where the workaround is to edit AVAudioSessionCategoryPlayback and work with the UIBackgroundTaskIdentifier. The problem for me is (just like in any other fix I found so far) that those solutions always assume that the audio is played either with AVaudioPlayer or MPMusicPlayerController. But in my case I user neither, our audio is played by our html5 player wrapped in UIwebView.

Anyone has any advice on how to continue playing the audio in iOS multitask mode when the audio player is a html5/javascript player?

like image 638
TanjuNYC Avatar asked Jan 09 '12 02:01

TanjuNYC


People also ask

Can Apple music play in the background?

Under the Capabilities tab, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in Picture” option under the list of available modes. With this mode enabled and your audio session configured, your app is ready to play background audio.

Can I play audio in HTML5?

HTML5 features include native audio and video support without the need for Flash. The HTML5 <audio> and <video> tags make it simple to add media to a website. You need to set src attribute to identify the media source and include a controls attribute so the user can play and pause the media.

What is background audio?

By definition, background sound is not foreground sound, meaning that you are not purposefully listening to it or gaining information from it.


1 Answers

My app plays audio via an <audio> tag in html hosted in UIWebView, and it supports background playing.

As you suggest, you need to have the 'App plays audio' background mode defined in your plist.

Try adding this to your applicationDidFinishLaunching: handler:

NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
like image 90
TomSwift Avatar answered Oct 21 '22 11:10

TomSwift