Xcode: Play an audio file with music on the background - ios

I've build an iOS application with Cordova 3.6.3 that uses interface sounds. When you click on buttons the application plays a short "click"-sound. This works great. But when I use my application while music is playing on the background from another app this happens:
1. The background music fades out.
2. The interface sound is played.
3. The background music fades in.
This is not how it should happen. I would like to play interface-sounds while the background music is still on. What do I need to change to Cordova / Phonegap to change this behavior? Something with AVAudioSession?
Thank you for helping.

I've found the solution in CDVSound.m.
Old version
NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryPlayback : AVAudioSessionCategorySoloAmbient;
[self.avSession setCategory:sessionCategory error:&err];
New version
NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryAmbient : AVAudioSessionCategoryAmbient;
[self.avSession setCategory:sessionCategory error:&err];
Check https://developer.apple.com/Library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionBasics/AudioSessionBasics.html for more info.

Related

Play youtube video in Background using YouTube Helper Library iOS

Im using YouTube Helper Library to play YouTube videos on my iOS application
Here are link of Library
I just need a way to continue audio (or video) playing in background ,
So when user click on home button i need him to be able to hear the voice
of the video ,
even when he lock his device i need to him to be able to control the sound and
click next and back button for audio playing ..
How i can achieve that on YouTube Helper Library ?
NOTE: many application do that and they exist on app-store like iMusic , Video Tube ,MusicTV , MB2
Playing background video (or voice) is against the YouTube TOS. You can not do it with this library for sure, even if you figure out a workaround, your app would be taken down because of TOS violation.
About playing sounds in background:
You have to set the AVAudioSession category to AVAudioSessionCategoryPlayback and add the audio value to the UIBackgroundModes key in your information property list file.
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
I have tested this code on iOS 7 and up only, so it may not work for earlier versions.
From Apple docs:
AVAudioSessionCategoryPlayback The category for playing recorded music
or other sounds that are central to the successful use of your app.
When using this category, your app audio continues with the Silent
switch set to silent or when the screen locks. (The switch is called
the Ring/Silent switch on iPhone.) To continue playing audio when your
app transitions to the background (for example, when the screen
locks), add the audio value to the UIBackgroundModes key in your
information property list file.
see references here and here
You need to enable it when app enter background. Remember to check your app is in background
func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
if state == .paused {
playerView.playVideo()
}
}

MPMusicPlayerController Play music while app is in background

Hope this question doesn't get down voted - I haven't seen anything in the documentation.
I want to play music from the ipod library while the app is in the back ground.
I am creating the music player using the default mechanism.
-(void)viewWillAppear:(BOOL)animated
{
MPMusicPlayerController *myPlayer =
[MPMusicPlayerController applicationMusicPlayer];
// assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
// start playing from the beginning of the queue
[myPlayer play];
}
As soon as the app enters the background the music stops playing. I would like it to continue playing though.
I do not want to use the iPodMusicPlayer option
How can I do this?
You should set background audio mode in the project settings (Project settings > Capabilities > Background modes > Audio and AirPlay) like it's shown on the screenshot below:
Also you should set Audio Session Category in the appDelegate (in the didFinishLaunching method). Example is shown below:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
I have same issue. I searched even after added flag in plist. It will not work because :
The applicationMusicPlayer does not support background music. Use
MPMusicPlayerController's systemMusicPlayer instead. It shares the
state with the built-in System player and music will continue to play
when your app enters the background.
Other Solution is :
You can use AVAudioPlayer to play either streamed or local(bundle) audio. It even allows you to play multiple audio file at the same time having an AVAudioPlayer instance for each.
Note : iPodMusicPlayer is deprecated. so Instead of this use systemMusicPlayer.

Sound effect that quiets other audio iOS AVAudio

I want to have a sound effect in my iOS that, if other audio is playing (such as iTunes) quiets any other audio is playing and plays over top of it. I know
AVAudioSessionCategoryOptionMixWithOthers
Will mix the sounds together, but I want mine to be able to be heard clearly. I've seen this done in the RunKeeper app (when the lady describes your run to you it quiets your music) but I can not find it in Apple's Documentation.
Anyone have experience with this?
Apple's documentation says this only works with PlayAndRecord, but it works.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error: nil];

Audio triggered by background location

Target OS: iOS 6+
Devices: Phones only
I'm creating an fairly simple locative media app. The one technical difficulty I'm having is whether or not I can do what I want with background audio.
I receive GPS location updates successfully in the background but I would like to use these to trigger audio on region entry. The app does not play audio constantly in foreground, only on entry to these fences.
Does the 'playing silence' trick still work to keep the Audio Session and is this an app where Apple would tolerate its use? If so could anyone provide recently functional code for it?
If you set up your application to also enable background audio, in application:didFinishLaunching: like so:
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
and add audio as a background mode in the app's plist file, your app will keep on running in the background and play sounds when it wishes (e.g. when a location change has happened). However, I think you have to close the application while playing sound.
However, there might be an issue with this, because background audio is meant for apps that actually keep running a track/stream in the background. I could see how Apple might reject your app for using their API in an unintended way. So this is the problem with the playing silence trick.
Also, another problem could be that if the user goes out of your app, then starts playing their music through Music or any other music app (Spotify, a radio app etc.) that has access to background audio, your application's audio permissions will be disabled (your app won't be able to play sound anymore through the audio framework, because another app has taken control of the background audio).
An option I would recommend: throw a local notification with your own sound when the user changes location:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.soundName = #"your-sound-file-name";

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?
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];
Thanks, guys, but I ended up simply keeping the app busy in a loop when in background mode. This was enough to bridge the time when connecting to the next song on the playlist.
The code I used is similar to this one:
iPhone - Backgrounding to poll for events
You could try this, adding a function on the onend event.

Resources