UIEventSubtypeRemoteControlTogglePlayPause not doing anything - ios

I have an application that plays music and want to use lockscreen control (play/pause).
With NSLog I can see that my app get's the button trigger but not theUIEventSubtypeRemoteControlTogglePlayPause.
Here's a bit of my code:
- (void)viewDidLoad {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
NSLog(#"REMOTE RECEIVE");
if (receivedEvent.type == UIEventTypeRemoteControl)
{
NSLog(#"received remote event");
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(#"toggle button received");
//[self togglePlayPauseTapped: nil];
break;
default:
break;
}
}
I get "REMOTE RECEIVE" and "received remote event" from the NSLog Output but not the line inside ...TogglePlayPause.
Any ideas?

use the case
UIEventSubtypeRemoteControlPause
UIEventSubtypeRemoteControlPlay
for iOS 7

The accepted answer is not clear.
UIEventSubtypeRemoteControlPlay, UIEventSubtypeRemoteControlPause and UIEventSubtypeRemoteControlStop are called within the user interaction.
UIEventSubtypeRemoteControlTogglePlayPause is called within headset interaction.

Related

Swift. Receive remote control events to work with MPNowPLayingInfoCenter

As I understand, in order to show music player on lock screen, writing the following code is not enough.
override func viewDidAppear(animated: Bool) {
var mpic = MPNowPlayingInfoCenter.defaultCenter()
mpic.nowPlayingInfo = [
MPMediaItemPropertyTitle:"This Is a Test",
MPMediaItemPropertyArtist:"Matt Neuburg"
]
}
My app also should be able to receive remote control events
So, how to do that in Swift?
I found this from Apple Documentation, but it's for Objective-C.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Set itself as the first responder
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
// Turn off remote control event delivery
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
// Resign as first responder
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playOrStop: nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previousTrack: nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextTrack: nil];
break;
default:
break;
}
}
}
Just found solution on GitHub
https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch14p643ducking/ch27p912ducking

Use Built-in Audio Controls with cocoalibspotify on iOS

How can I make my cocoalibspotify app for iOS respond to the built-in pause/play and next/previous controls? I see no documentation of this.
In the main view controller, the app needs to subscribe to remote control events.
- (void)viewDidLoad {
// ...
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// ...
}
Remote control button presses will then trigger remoteControlReceivedEvent: on your view controller. The event can be handled as follows.
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPause:
// Handle pause
break;
case UIEventSubtypeRemoteControlPlay:
// Handle play
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// Handle Previous
break;
case UIEventSubtypeRemoteControlNextTrack:
// Handle Next
break;
default:
break;
}
}
}

Remote control events in UITabBar to control play

This is a similar question to this. But I still can't quite figure out what to do.
So, I have a tabbar app that functions similar to an ipod. One view controller is the "NOW PLAYING" view controller and it is the view controller at index 1. So, in that VC I have:
- (void)viewDidAppear:(BOOL)animated
{
// Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
// Set itself as the first responder
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
NSLog(#"Where is my event?");
if(event.type == UIEventTypeRemoteControl)
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(#"Pause");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(#"Next");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(#"Prev");
break;
default:
NSLog(#"SOMETHING WAS CLICKED");
break;
}
}
}
I don't receive any events on remote clicks either on the headphones or on the double-home button click shortcuts. I am running on an actual iPhone, not in the simulator. I am using a AVQueuePlayer (which IS playing) to manage my media.
I just shoved everything into the AppDelegate and told the AppDelegate to call to the ViewController to tell it what to do and it worked fine. Is it poor practise to put it in the AppDelegate?

iPhone headset play button click

I want to detect headset button click on iPhone, see Detect headset button click on iPhone SDK.
I follow as the http://developer.apple.com/library/IOS/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html,
then I wrote code as below, but it cannot work!!
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
if (receivedEvent.type == UIEventTypeRemoteControl) {
NSLog(#"Remote Control Event");
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(#"UIEventSubtypeRemoteControlTogglePlayPause");
break;
default:
break;
}
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
My Problem is that I cannot catch remote control event. When play or pause button on headset clicked, console prints nothing! Who can help me? Thank you.
Is your Info.plist updated to indicate that you support audio?
From http://www.iphonedevsdk.com/forum/iphone-sdk-development/44433-there-way-respond-clicks-headphone-buttons.html
Add the new line, and select "Required Background Modes". In the "Item 0" that appears under/next to it, select "App plays audio".

Unable to handle remote control events for backgronud audio

In the view controller for my audio player I've added this:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
This switches the icon in the player controls from the iPod icon to my own app, it also puts the little playback icon in the status bar.
Next I added this to my view controller to handle the remote events:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
NSLog(#"REMOTE EVENT!");
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[streamer pause];
break;
case UIEventSubtypeRemoteControlPlay:
[streamer start];
break;
case UIEventSubtypeRemoteControlPause:
[streamer pause];
break;
case UIEventSubtypeRemoteControlStop:
[streamer stop];
break;
default:
break;
}
}
However, this is never called. I tried bringing up the playback controls while the app was running, I tried going back to my home screen and tapping some playback controls, and I tried my earbuds' controls. All had no luck.
Does anyone have any pointers as to where I can be going wrong?
Thanks.
You have implemented the wrong method: instead of - (void) viewWillAppear:(BOOL)animated it should have been - (void)viewDidAppear:(BOOL)animated.
I ran into this problem before and discovered that in my problem it was because my view was a subview and the parent was catching the remote events. I would start in the AppDelegate and implement the method and see if anything is being sent to the AppDelegate and then trace it down from there. Chances are there is a view or viewController that is catching the event and not passing it along.
Have you made sure to set your audio session? Use AVAudioSession and set the category to AVAudioSessionCategoryPlayback before becoming first responder.

Resources