Search code examples
iosobjective-cmpnowplayinginfocenter

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.


Solution

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

    }