【问题标题】:beginReceivingRemoteControlEvents not triggering events for Apple MusicbeginReceivingRemoteControlEvents 不触发 Apple Music 的事件
【发布时间】:2016-04-25 07:16:00
【问题描述】:

我正在从我的应用程序播放 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;
    }
}

注意-如果iTunes 曲目正在由AVPlayer 播放或Spotify 曲目正在由Spotify iOS SDK 播放,则remoteControlReceivedWithEvent 每次都会触发。

但在播放 Apple Music 曲目时不会触发相同的代码 -

[MPMusicPlayerController applicationMusicPlayer]      

                 or

 [MPMusicPlayerController systemMusicPlayer]

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

【问题讨论】:

  • 也在尝试解决这个问题!
  • 嘿,你找到解决办法了吗?

标签: ios objective-c mpmusicplayercontroller apple-music


【解决方案1】:

不要使用-(void)remoteControlReceivedWithEvent:(UIEvent *)event 来跟踪控件,而是使用MPRemoteCommandCenter 为每个控件添加目标:

注意:在分配目标之前启用控件很重要。

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

【讨论】:

  • 您在使用 MPMusicPlayerController 时调用了这些函数?我不能让它开火。
  • 尝试使用 - (BOOL)canBecomeFirstResponder { return true; 启用您的 ViewController 作为第一响应者; }
  • 嗯试过了,但我要再试一次。我只是想确保你真的让它工作了吗?
  • 我已经使用了那里的功能,但没有在我的应用程序中工作。
  • 我已经向苹果提交了一份错误报告。有人有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2015-09-20
  • 2018-10-11
  • 2020-07-15
  • 1970-01-01
  • 2015-09-30
相关资源
最近更新 更多