4

I'm using this below code. And this code is working perfectly, once application goes in background. If app not goes in background and we try to play and pause currently playing song, then play and pause image is not change. Ad every time breakpoint goes in this case UIEventSubtypeRemoteControlPause: case.

albumArt= [[MPMediaItemArtwork alloc] initWithImage: img.image];
            NSDictionary *playingNowInfo = @{MPMediaItemPropertyTitle: (self.currentItem.songname==nil) ? (@"") : (self.currentItem.songname),
                                             MPMediaItemPropertyPlaybackDuration: @(self.currentItem.duration),
                                             MPMediaItemPropertyArtist:[MusicSetting getArtistName],
                                             MPMediaItemPropertyAlbumTitle:(self.currentItem.albumname==nil) ? (@"") : (self.currentItem.albumname),
                                             MPNowPlayingInfoPropertyPlaybackRate: @(self.player.rate),
                                             MPNowPlayingInfoPropertyElapsedPlaybackTime: @(CMTimeGetSeconds(self.player.currentItem.currentTime)),
                                             MPMediaItemPropertyArtwork:albumArt,
                                             };
            [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = playingNowInfo;

#pragma mark - Remote Control

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

  //  [self.manager remoteControlReceivedWithEvent:receivedEvent];
    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlPlay:
                [self Onclick_Play_Pause:self];
                break;

            case UIEventSubtypeRemoteControlPause:
                [self Onclick_Play_Pause:self];
                 break;

            case UIEventSubtypeRemoteControlTogglePlayPause:

                //if ([self.manager.player isPlaying])
                if(self.manager.player.rate != 0)
                {
                    [self.manager.player pause];
                }

                else {
                    [self.manager.player play];
                }
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [self Onclick_next:self];
                NSLog(@"Next song play");
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [self Onclick_prev:self];
                NSLog(@"Prev song play");
                break;

            default:
                break;
        }
    }
}

- (IBAction)Onclick_Play_Pause:(id)sender {
    (AppObj).playerview_height=playviewHeight;

    if(self.manager.player.rate != 0)
    {
        [MusicSetting set_SongStatus:@"Pause"];
        [self.Play_PauseBtn setImage:[UIImage imageNamed:@"pauseImg"] forState:UIControlStateNormal];
        [self.manager pause];
    }
    else {
        [MusicSetting set_SongStatus:@"Play"];
        [MusicSetting set_isMusicPlay:@"Playing"];

        [self.Play_PauseBtn setImage:[UIImage imageNamed:@"playImg"] forState:UIControlStateNormal];

        [self.manager play];
    }
}

Thanks!

Yogendra Patel
  • 813
  • 2
  • 8
  • 24

3 Answers3

0

Hi Please check have added the following in info.plist

info.plist addition

Surbhi Garg
  • 414
  • 5
  • 19
0

You have following code

 case UIEventSubtypeRemoteControlPlay:
            [self Onclick_Play_Pause:self];
            break;

        case UIEventSubtypeRemoteControlPause:
            [self Onclick_Play_Pause:self];
             break;

        case UIEventSubtypeRemoteControlTogglePlayPause:

            //if ([self.manager.player isPlaying])
            if(self.manager.player.rate != 0)
            {
                [self.manager.player pause];
            }

            else {
                [self.manager.player play];
            }
            break;

In the first two cases you called your method but not in the third one can you share the definition of your method?

Surbhi Garg
  • 414
  • 5
  • 19
0
- (IBAction)Onclick_Play_Pause:(id)sender {
     MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
        NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];
    (AppObj).playerview_height=playviewHeight;

    if(self.manager.player.rate != 0)
    {
        [MusicSetting set_SongStatus:@"Pause"];
        [self.Play_PauseBtn setImage:[UIImage imageNamed:@"pauseImg"] forState:UIControlStateNormal];
        [self.manager pause];
         //set playback rate
        [playingInfo setObject:[NSNumber numberWithFloat:0] forKey:MPNowPlayingInfoPropertyPlaybackRate];
    }
    else 
    {
        [MusicSetting set_SongStatus:@"Play"];
        [MusicSetting set_isMusicPlay:@"Playing"];

        [self.Play_PauseBtn setImage:[UIImage imageNamed:@"playImg"] forState:UIControlStateNormal];
        [self.manager play];
        //set playback rate
        [playingInfo setObject:[NSNumber numberWithFloat:1] forKey:MPNowPlayingInfoPropertyPlaybackRate];
    }
     center.nowPlayingInfo = playingInfo;

}
Monika Patel
  • 2,287
  • 3
  • 20
  • 45