I have done a lot of research, both on Google and StackOverflow. All the answers I found do not work in iOS 7. I started writing fresh app in iOS 7 SDK with Xcode 5.
All I'm trying to do is play audio in the app from a file stored in the app bundle (not from the Music library). I want to have audio played in background and controlled when screen is locked (in addition to Control Center).
I set the APPNAME-Info.plist
key, UIBackgroundModes
, to audio. It is not handling things in the app delegate; everything is done inside the ViewController
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
Within the implementation's viewDidAppear:
method I call super and then the following code:
// Once the view has loaded then we can register to begin receiving controls and we can become the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
In my implementation's viewWillDisappear:
method, I have the following code:
// End receiving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
I have also implemented the canBecomeFirstResponder
method, which returns YES. Next, I implemented the remoteControlReceivedWithEvent:
method:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
// If it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
[self playPauseAudio:self];
} else if (event.subtype == UIEventSubtypeRemoteControlPause) {
[self playPauseAudio:self];
} else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
[self playPauseAudio:self];
}
}
}
What is confusing me is that this exact same setup was working fine on iOS 6. On iOS 7, it doesn't work. It used to be so easy in iOS 6. Something fundamentally changed in iOS 7 SDK. What am I missing?
If you want to play audio in background in iphone and simulator also then you need to write this code in plist and Firstly make sure your Info.plist correctly lists audio as a background mode.
(If you dont know what i'm talking about select YOURAPPNAME-Info.plist select that. Click on the plus sign and type a key UIBackgroundModes and enter. Add a value called "App plays audio"(for simulator) or "App plays audio or streams audio/video using AirPlay"(For iphone).)
in AppDelegate.m
Add these two framework in your project and some line of code in ViewController.h
Remind that these frameworks refrences should be added in your project.
Then in Viewcontrller.m
I hope it will help you to play audio in background in iphone and simulator as well.
Apparently the problem was on Apple's side as iOS update 7.0.3 fixes this issue. Besides what Alex noted about UIEventSubtype changes the code that worked on iOS6 now works on iOS7.
For sake of completeness, here is my relevant code that is working in both iOS6 and iOS7 - after the udpate to 7.0.3. Also included AVFoundation.framework and MediaPlayer.framework in project Build Phases -> Link binary with libraries. No code for this in app delegate.
In viewcontroller .h file:
In viewcontroller .m file:
I managed to solve this, and to save hair pulling by another poor soul here goes:
Firstly make sure your Info.plist correctly lists audio as a background mode.
(If you dont know what i'm talking about select YOURAPPNAME-Info.plist select that. Click oin the plus sign and add a new key called
UIBackgroundModes
and expand it. Add a value calledaudio
.)You'll need a reference to whatever playback object is creating the audio. Since I'm only playing audio and AVplayer was not abiding by the background audio, use this in your view controller's header:
In the implementation, do the following:
and
add two methods
now ALL important code - this enables your app to control audio from "control center" and from lock screen:
you can add many many types of Event types here and call any method.
Typical events are:
To Debug help you can use:
and thats it - hope it helps some one out there! - this is working perfect on iOS 7 and iOS 6 with Storyboard app as well as control using Headphone and all new control centre too.
Since I am also interested in finding this solution I will add some more information from my side.
I am experiencing the same problem, but still Apple documentation hasn't changed about remote control event management.
I tried moving some stuff and something interesting happened.
I originally had the remote control event management in my TabBar controller. Now that i moved everything in the player view controller, I can see that I can again control the music playback with my headset but not from the buttons in the new control panel of iOS7 (the one that come from the bottom on the screen).
This is quite weird.
In order to solve the problem, let's try to enrich the thread with our test.
One thing to note that is different from iOS6 to iOS7 with remote control events is that in iOS6 the play/pause events came as one
UIEventSubtype
:UIEventSubtypeRemoteControlTogglePlayPause
And in iOS7 they come as two separate subtypes:
UIEventSubtypeRemoteControlPause
UIEventSubtypeRemoteControlPlay