i tried to look for it for days in google and i couldn't find an answer.
i have an app that play audio stream from the internet and i use MPNowPlayingInfoCenter to display the artist title, song title and artwork. now my question is how to use the play/pause button in the MPNowPlayingInfoCenter to play or stop my audio stream.
For this you have to handle remote control Events --
//Add these lines in viewDidAppear()
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
//Add these lines in viewWillDisappear()
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
Then Use
-(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
NSLog(#"received event!");
if (receivedEvent.type == UIEventTypeRemoteControl)
{
switch (receivedEvent.subtype)
{
case UIEventSubtypeRemoteControlPlay:
// play the video
break;
case UIEventSubtypeRemoteControlPause:
// pause the video
break;
case UIEventSubtypeRemoteControlNextTrack:
// to change the video
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// to play the privious video
break;
default:
break;
}
}
}
Related
I have tried setting the playinginfo using the below method :
-(void)handleTheMediaPropertyList
{
UIImage *artWork = [UIImage imageNamed:#"sample.png"];
NSDictionary *nowPlaying = #{MPMediaItemPropertyArtist: #"SAMPLE TEST",
MPMediaItemPropertyAlbumTitle: #"SAMPLE TEST",
MPMediaItemPropertyArtwork:[[MPMediaItemArtwork alloc] initWithImage:artWork]
};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
break;
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
default:
break;
} }
}
After playing the audio, whenever I go to the control center, playing info shows fine there. But when I PAUSE it playing info gets changed to and instead of showing Artist name which I had set above it shows the URL of the link where it is getting streamed from. And later after pausing then playing it again then same data is showed.
So for the first time when the audio is played the Playinfo shows the proper data, but once I open the control center and press PAUSE and PLAY then the playing info gets changed.
Please help me with this.
I added the Spotify player to my app which also plays music using the MPMusicPlayerController. When music is playing from Spotify and the screen is locked, the remote control events are not received for play/pause and FFW/RWD when the user presses these buttons on the locked screen.
If music is playing from the MPMusicPlayerController, I am able to receive the remote control events based on the following code:
-(void) ViewDidLoad {
...
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
...
}
and
- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (void) remoteControlReceivedWithEvent: (UIEvent*) event
{
// see [event subtype] for details
if (event.type == UIEventTypeRemoteControl) {
// We may be receiving an event from the lockscreen
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
// User pressed play or pause from lockscreen
[self playOrPauseMusic:nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
// User pressed FFW from lockscreen
[self fastForwardMusic:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// User pressed rewind from lockscreen
[self rewindMusic:nil];
break;
default:
break;
}
}
}
While the iPod controls are visible when the app enters the background, they do not respond when I press pause. Instead, the iPod controls disappear when I press pause. What addition is needed to enable detection of play/pause and FFW/RWD when streaming audio such as Spotify is playing in the background from lock screen?
I believe I ran into this in the past. If I remember correctly I added in the
-(void)remoteControlReceivedWithEvent:(UIEvent *) event { ... }
as well as
- (BOOL) canBecomeFirstResponder { return YES; }
to the app delegate (This is also where my audio controller lived). I was having having the issue where the UIViewControllers were not alive during the time I wanted to catch the UIEventTypeRemoteControl notifications.
Give that a try and see if that helps.
After further investigation, I have found that if include the following code when my app enters the background and when the remote control events are received, the iPod controls do not disappear.
// Set up info center to display album artwork within ipod controls (needed for spotify)
MPMediaItemArtwork *ipodControlArtwork = [[MPMediaItemArtwork alloc]initWithImage:artworkImage];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:nowPlayingTitle, MPMediaItemPropertyTitle,
nowPlayingArtist, MPMediaItemPropertyArtist, ipodControlArtwork, MPMediaItemPropertyArtwork, [NSNumber numberWithDouble:0.0], MPNowPlayingInfoPropertyPlaybackRate, nil];
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.
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;
}
}
}
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".