Playinfo changes when I click on Play or Pause - MPNowPlayingInfoCenter - ios

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.

Related

Lock Screen iPod Controls Not Working With Spotify Music Player

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];

podcast "skip" buttons in iOS lock screen

I have an audio app that plays background audio on iOS devices. I need to have the app have the "skip 15" buttons — a la the Apple Podcasts app and Overcast — instead of next/previous track buttons. Does anyone know where the documentation to this is, or of some examples? This is turning out to be a tricky issue to Google.
Update: Great answer to this question for iOS 7.1 and later at https://stackoverflow.com/a/24818340/1469259.
The buttons on the lock screen trigger "remote control" events. You can handle these events and skip forward/back as you require:
- (void)viewDidAppear:(BOOL)animated {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
if ([self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
// add code here to play/pause audio
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// add code here to skip back 15 seconds
break;
case UIEventSubtypeRemoteControlNextTrack:
// add code here to skip forward 15 seconds
break;
default:
break;
}
}
}
How you do the actual skipping depends on how you're playing the audio.
Documentation here https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Remote-ControlEvents/Remote-ControlEvents.html
Potentially another way of achieving this, but not one I've used myself is MPSkipIntervalCommand https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPSkipIntervalCommand_Ref/index.html

how to overide play/pause button in MPNowPlayingInfoCenter

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;
}
}
}

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?

Resources