4

我正在使用下面的代码。一旦应用程序进入后台,这段代码就可以完美运行。如果应用程序没有进入后台并且我们尝试播放和暂停当前正在播放的歌曲,则播放和暂停图像不会改变。在这种情况下,每次断点都会出现广告case UIEventSubtypeRemoteControlPause:

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

谢谢!

4

3 回答 3

0

嗨,请检查是否在 info.plist 中添加了以下内容

info.plist 添加

于 2016-08-02T09:20:27.607 回答
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;

}
于 2016-08-03T04:31:33.293 回答
0

您有以下代码

 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;

在前两种情况下,您调用了您的方法,但在第三种情况下没有,您可以分享您的方法的定义吗?

于 2016-08-02T09:59:51.007 回答