ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件

前端之家收集整理的这篇文章主要介绍了ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从我的应用程序播放Apple Music,苹果音乐播放器代码如下 –
-(void) submitAppleMusicTrackWithProductID: (NSString *) productID // productID in US is the last numbers after i= in the share URL from Apple Music
{

    [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
        NSLog(@"status is %ld",(long)status);
        SKCloudServiceController *cloudServiceController;
        cloudServiceController = [[SKCloudServiceController alloc] init];
        [cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities,NSError * _Nullable error) {
            NSLog(@"%lu %@",(unsigned long)capabilities,error);

            if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary || capabilities==SKCloudServiceCapabilityMusicCatalogPlayback)
            {
                NSLog(@"You CAN add to iCloud!");
                [[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:productID completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull           entities,NSError * _Nullable error)
                 {
                     NSLog(@"added id%@ entities: %@ and error is %@",productID,entities,error);
                     NSArray *tracksToPlay = [NSArray arrayWithObject:productID];
                 [[MPMusicPlayerController applicationMusicPlayer] setQueueWithStoreIDs:tracksToPlay];
                    [[MPMusicPlayerController applicationMusicPlayer] stop];
                 [[MPMusicPlayerController applicationMusicPlayer] play];
                   self.isTrackChangedByNextPrevIoUsButton = NO;

        [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                              selector:@selector(handleTrackChanged:)
                                                                  name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                                                object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                              selector:@selector(handlePlaybackStateChanged:)
                                                                  name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                                                object:nil];


                     [[MPMusicPlayerController applicationMusicPlayer] beginGeneratingPlaybackNotifications];
                     [[MPMusicPlayerController applicationMusicPlayer] setRepeatMode: MPMusicRepeatModeNone];
                 }];


            }
            else
            {
                NSLog(@"Blast! The ability to add Apple Music track is not there. sigh.");
            }

        }];

    }];
}

-(void)handleTrackChanged:(id )notification
{
    if (!self.AppleMusicPreviuosTrack)
    {        
        self.AppleMusicPreviuosTrack = [[Tracks alloc] init];
    }
    if (self.AppleMusicPreviuosTrack.trackId == self.CurrentTrack.trackId && [MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate == 0 && !self.isSongChangedManually)
    {
        self.isSongChangedManually = YES;
        [self FilterArtistsTracks:@"next" :^(Tracks *track,NSError *err)
         {

         }];

    }
    else
    {
     if ([[MPMusicPlayerController applicationMusicPlayer] currentPlaybackRate]==1)
     {
    self.AppleMusicPreviuosTrack.trackId = self.CurrentTrack.trackId;
    [[NSNotificationCenter defaultCenter] postNotificationName:kTrackChanged object:nil];
         //Delay execution of my block for 20 seconds.
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW,30 * NSEC_PER_SEC),dispatch_get_main_queue(),^{
     self.isSongChangedManually = NO;
 });

     }
    }
}

-(void)handlePlaybackStateChanged:(id )notification
{
    NSLog(@"handle_PlaybackStateChanged");
     [[NSNotificationCenter defaultCenter] postNotificationName:kDidTrackPlaybackStatus object:nil];
    if ([MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate>0)
    {
        [self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    }
    else
    {
        [self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    }

}

这工作得很好.现在我想从锁定屏幕控制轨道,为此我在viewWillAppear中执行了以下代码

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
   [APP_DELEGATE becomeFirstResponder];

和remoteControlReceivedWithEvent方法在AppDelegate文件中编写如下 –

-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch (event.subtype)
    {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            [APP_DELEGATE PlayPauseMusic:nil];
            //[streamer pause];
            break;
        case UIEventSubtypeRemoteControlPlay:
            [APP_DELEGATE PlayPauseMusic:nil];
            //[streamer start];
            break;
        case UIEventSubtypeRemoteControlPause:
            [APP_DELEGATE PlayPauseMusic:nil];
            //[streamer pause];
            break;
        case UIEventSubtypeRemoteControlStop:
            [APP_DELEGATE PlayPauseMusic:nil];

            //[streamer stop];
            break;
        case UIEventSubtypeRemoteControlNextTrack:

            [APP_DELEGATE next:nil];

            break;
        case UIEventSubtypeRemoteControlPrevIoUsTrack:
            [APP_DELEGATE prevIoUs:nil];


            break;

        default:
            break;
    }
}

注 – 如果AVPlayer正在播放iTunes曲目,则每次都会触发remoteControlReceivedWithEvent.Spotify iOS SDK会播放Spotify曲目.

但是,播放Apple Music曲目时,相同的代码不会触发 –

[MPMusicPlayerController applicationMusicPlayer]      

                 or

 [MPMusicPlayerController systemMusicPlayer]

任何帮助将不胜感激.谢谢

解决方法

而不是使用 – (void)remoteControlReceivedWithEvent:(UIEvent *)事件来跟踪控件,使用MPRemoteCommandCenter向每个控件添加目标:

Note: It’s important to enable the controls before assigning a target.

[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];

[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)];

[MPRemoteCommandCenter sharedCommandCenter].prevIoUsTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].prevIoUsTrackCommand addTarget:self action:@selector(loadPrevIoUsSong)];

[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)];

选择:

-(void) remotePlay {
    [APP_DELEGATE PlayPauseMusic:nil];
}
-(void) remoteStop {
    [APP_DELEGATE PlayPauseMusic:nil];
}
-(void) loadNextSong {
    [APP_DELEGATE next:nil];
}
-(void) loadPrevIoUsSong {
    [APP_DELEGATE prevIoUs:nil];
}
原文链接:https://www.f2er.com/iOS/333972.html

猜你在找的iOS相关文章