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

ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件,第1张

概述我正在从我的应用程序播放Apple Music,苹果音乐播放器代码如下 – -(void) submitAppleMusicTrackWithProductID: (NSString *) productID // productID in US is the last numbers after i= in the share URL from Apple Music{ [SKClo 我正在从我的应用程序播放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];}
总结

以上是内存溢出为你收集整理的ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件全部内容,希望文章能够帮你解决ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104113.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存