iOS6 Background Audio Overlap - ios

I have an iOS app made with Pphonegap 1.8.1 and Sencha Touch 2 that plays HTML5 audio. I've set UIBackgroundModes -> audio in my info.plist and followed this answer to allow background audio on iOS6.
My issue is that whenever I play audio in the background, it will persist and overlap with previously played tracks. To reproduce:
Play HTML5 audio inside an app on an iOS 6 device.
Toggle the display off and on, so that you are on the lock screen (audio continues to play as expected).
Unlock and return to the app.
Stop the HTML5 audio and play another track.
Toggle the display off and on, so that you are on the lock screen (audio continues to play as expected).
Double tap the home button to display the audio controls on the lock screen, and tap the pause button.
Result:
The currently playing audio is paused but the previous audio (from step 1) now starts to play. If you tap the next track button, then the previous track, and then tap play, it will play both audio items at the same time.
Note:
On iOS 5.1, previously played background audio is not persisted.
Is there a way I can ensure only the last played audio from the app is available in the background on iOS 6?

Related

Playing audio in a WatchKit workout app when backgrounded and screen is off

I'm trying to build a WatchKit app that will give audio feedback during a workout. The audio is a series of short pre-recorded voice clips.
The app starts a workout session
Audio clips play fine when the app is active
Audio also plays fine when the app is in the background whilst the screen is on (e.g. by tapping the digital crown, or opening another app).
However, when the screen turns off - e.g. by starting a workout and then lowering the wrist - the audio doesn't play until the app becomes active again.
i.e.
Start workout + keep app on screen = sound plays ok.
Start workout + move app to background by tapping digital crown + keep screen on = sound plays ok.
Start workout + lower wrist = screen switches off and sound no longer plays.
Start workout + move app to background + lower wrist = screen switches off and sound no longer plays.
I'm trying to figure out what I've missed in order to support audio in a workout app when the screen is off...
The app has an active HKWorkoutSession.
The extension has the "workout-processing" background mode enabled under WKBackgroundModes
The extension also has the "audio" background mode enabled under UIBackgroundModes.
The extension has the HealthKit entitlement and all HealthKit features are working.
On the iOS app, I've added the 'audio' background mode.
I've tried:
playing the audio clips using AVAudioPlayer
playing audio using AVAudioEngine+AVAudioPlayerNode
giving up on the clips entirely and using AVSpeechSynthesizer
The behaviour is the same - as soon as the screen switches off, the sound no longer plays. The sound does play if the app is in the background and the screen is still on.
The app is running on Watch OS 4. I've tested on a Series 2 and a Series 3 watch and it is the same on both.
What have I missed?
Ahhh, finally I found this post and realised I had the same problem:
The AVAudioSession category needs to be set to AVAudioSessionCategoryPlayback (I had it set to ambient).
As soon as I changed the session category to AVAudioSessionCategoryPlayback it worked!
To play audio while in background you also need to set the UIBackgroundModes to "audio" in the extension plist apart from setting the "workout processing" background mode.
Refrence HKWorkoutSession
To play audio or provide haptic feedback from the background, you must also add the UIBackgroundModes key to your WatchKit extension’s Info.plist file. This key’s value is an array containing the audio value.

HTML5 Audio strange behavior with iOS lock screen

I have a website that uses html5 audio blocks to play audio. When I drag the progress bar to jump to another part of the track, the timer stops incrementing and the play/pause button stops working. Does anybody know what properties on an HTML5 audio block the iOS lock screen is reading to show the progress bar?

How can I play music successively in the background with UIWebView or WKWebView

The website implemented inside my app would play a series of mp3 files,it works well when the app is active,but when I press HOME button or lock the phone,it cannont play more than ONE song in the background,after that,app suspended.
I've already tried to set different kind of AVAudioSession Category,but of no use.
I assume you don't have audio background mode enabled. This is what Apple documentation says:
When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content or recording audio content, the app continues to run in the background. However, if recording or playback stops, the system suspends the app.
So it seems like you just need to enable audio background mode. Here is Ray Wenderlich's tutorial on background modes

How to continue playing a youtube video at background when switch from my app back to home screen

Seems the default action for iOS 6 is to stop the video from playing after I switch to HOME screen. Then if I want to listen to the audio I have to go back to the app and press the 'Play' button again. I am using youtube API.
How can I continue playing the video when the application is in UIApplicationStateInactive and UIApplicationStateBackground. I just want to be able to listen to the audio even if I am on HOME screen or the screen is locked. Should I use a background thread or restart playing the video at the previous position as it was before turning the app inactive?
Take a look at the app 'Jasmine'. They managed to do something similar to what you are seeking. However, UIApplicationStateInactive/UIApplicationStateBackground will cause the video to pause. Double tapping the home button and then pressing the audio play button will cause the video's audio to resume without keeping the app active.
Take a look at the new iOS 7. You may have some luck there.
Inter-App Audio
Now your apps can make beautiful music together. With Inter-App Audio,
apps can register their audio streams to share with other apps. For
example, a series of apps could publish audio streams of instrument
tracks while another uses the combination of these streams to compose
a song. Inter-App Audio also provides for MIDI control of audio
rendering, remotely launching other registered Inter-App Audio apps
and more.

Airplay of audio using AVPlayer does not work in the background

I am doing audio playback using the AVPlayer class from a remote URL.
It plays fine if I just play to the headphones/built in speaker, but I am seeing some oddities with AirPlay.
When I change to use an AppleTV for AirPlay, it will stream the audio, but on my iphone the audio playback indicator (the small 'play' triangle in the top menu bar) disappears, and when I lock the phone instead of continuing in the background, it stops. (When playing normally, it plays in the background fine).
Why is the AVPlayer not working in the background for audio over AirPlay?
Is this what other see? Is there a way around it?
Ok - I dug through the apple dev forums and found a hint.
If you are using AVPlayer for audio only, and want it to work in the background while doing airplay, you need to disable the allowsAirPlayVideo setting.
Apparently AVPlayer on iOS 5+ assumes that it is playing back video via AirPlay, and so does not allow backgrounding, unless you explicitly disable video AirPlay.
Once you disable this (i.e. self.player.allowsAirPlayVideo = FALSE;) then your audio will still play via AirPlay, but now it will not be treated as video, only as audio, and so will allow the backgrounding to work.
Have you enable the 'Background Audio' mutlitasking setting in the app plist?

Resources