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

前端之家收集整理的这篇文章主要介绍了ios – beginReceivingRemoteControlEvents不会触发Apple Music的事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从我的应用程序播放Apple Music,苹果音乐播放器代码如下 –
  1. -(void) submitAppleMusicTrackWithProductID: (NSString *) productID // productID in US is the last numbers after i= in the share URL from Apple Music
  2. {
  3.  
  4. [SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
  5. NSLog(@"status is %ld",(long)status);
  6. SKCloudServiceController *cloudServiceController;
  7. cloudServiceController = [[SKCloudServiceController alloc] init];
  8. [cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities,NSError * _Nullable error) {
  9. NSLog(@"%lu %@",(unsigned long)capabilities,error);
  10.  
  11. if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary || capabilities==SKCloudServiceCapabilityMusicCatalogPlayback)
  12. {
  13. NSLog(@"You CAN add to iCloud!");
  14. [[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:productID completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities,NSError * _Nullable error)
  15. {
  16. NSLog(@"added id%@ entities: %@ and error is %@",productID,entities,error);
  17. NSArray *tracksToPlay = [NSArray arrayWithObject:productID];
  18. [[MPMusicPlayerController applicationMusicPlayer] setQueueWithStoreIDs:tracksToPlay];
  19. [[MPMusicPlayerController applicationMusicPlayer] stop];
  20. [[MPMusicPlayerController applicationMusicPlayer] play];
  21. self.isTrackChangedByNextPrevIoUsButton = NO;
  22.  
  23. [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
  24. [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
  25.  
  26. [[NSNotificationCenter defaultCenter] addObserver:self
  27. selector:@selector(handleTrackChanged:)
  28. name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
  29. object:nil];
  30. [[NSNotificationCenter defaultCenter] addObserver:self
  31. selector:@selector(handlePlaybackStateChanged:)
  32. name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
  33. object:nil];
  34.  
  35.  
  36. [[MPMusicPlayerController applicationMusicPlayer] beginGeneratingPlaybackNotifications];
  37. [[MPMusicPlayerController applicationMusicPlayer] setRepeatMode: MPMusicRepeatModeNone];
  38. }];
  39.  
  40.  
  41. }
  42. else
  43. {
  44. NSLog(@"Blast! The ability to add Apple Music track is not there. sigh.");
  45. }
  46.  
  47. }];
  48.  
  49. }];
  50. }
  51.  
  52. -(void)handleTrackChanged:(id )notification
  53. {
  54. if (!self.AppleMusicPreviuosTrack)
  55. {
  56. self.AppleMusicPreviuosTrack = [[Tracks alloc] init];
  57. }
  58. if (self.AppleMusicPreviuosTrack.trackId == self.CurrentTrack.trackId && [MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate == 0 && !self.isSongChangedManually)
  59. {
  60. self.isSongChangedManually = YES;
  61. [self FilterArtistsTracks:@"next" :^(Tracks *track,NSError *err)
  62. {
  63.  
  64. }];
  65.  
  66. }
  67. else
  68. {
  69. if ([[MPMusicPlayerController applicationMusicPlayer] currentPlaybackRate]==1)
  70. {
  71. self.AppleMusicPreviuosTrack.trackId = self.CurrentTrack.trackId;
  72. [[NSNotificationCenter defaultCenter] postNotificationName:kTrackChanged object:nil];
  73. //Delay execution of my block for 20 seconds.
  74. dispatch_after(dispatch_time(DISPATCH_TIME_NOW,30 * NSEC_PER_SEC),dispatch_get_main_queue(),^{
  75. self.isSongChangedManually = NO;
  76. });
  77.  
  78. }
  79. }
  80. }
  81.  
  82. -(void)handlePlaybackStateChanged:(id )notification
  83. {
  84. NSLog(@"handle_PlaybackStateChanged");
  85. [[NSNotificationCenter defaultCenter] postNotificationName:kDidTrackPlaybackStatus object:nil];
  86. if ([MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate>0)
  87. {
  88. [self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
  89. }
  90. else
  91. {
  92. [self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
  93. }
  94.  
  95. }

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

  1. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  2. [APP_DELEGATE becomeFirstResponder];

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

  1. -(void)remoteControlReceivedWithEvent:(UIEvent *)event
  2. {
  3. switch (event.subtype)
  4. {
  5. case UIEventSubtypeRemoteControlTogglePlayPause:
  6. [APP_DELEGATE PlayPauseMusic:nil];
  7. //[streamer pause];
  8. break;
  9. case UIEventSubtypeRemoteControlPlay:
  10. [APP_DELEGATE PlayPauseMusic:nil];
  11. //[streamer start];
  12. break;
  13. case UIEventSubtypeRemoteControlPause:
  14. [APP_DELEGATE PlayPauseMusic:nil];
  15. //[streamer pause];
  16. break;
  17. case UIEventSubtypeRemoteControlStop:
  18. [APP_DELEGATE PlayPauseMusic:nil];
  19.  
  20. //[streamer stop];
  21. break;
  22. case UIEventSubtypeRemoteControlNextTrack:
  23.  
  24. [APP_DELEGATE next:nil];
  25.  
  26. break;
  27. case UIEventSubtypeRemoteControlPrevIoUsTrack:
  28. [APP_DELEGATE prevIoUs:nil];
  29.  
  30.  
  31. break;
  32.  
  33. default:
  34. break;
  35. }
  36. }

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

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

  1. [MPMusicPlayerController applicationMusicPlayer]
  2.  
  3. or
  4.  
  5. [MPMusicPlayerController systemMusicPlayer]

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

解决方法

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

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

  1. [MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
  2. [[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];
  3.  
  4. [MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
  5. [[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)];
  6.  
  7. [MPRemoteCommandCenter sharedCommandCenter].prevIoUsTrackCommand.enabled = YES;
  8. [[MPRemoteCommandCenter sharedCommandCenter].prevIoUsTrackCommand addTarget:self action:@selector(loadPrevIoUsSong)];
  9.  
  10. [MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
  11. [[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)];

选择:

  1. -(void) remotePlay {
  2. [APP_DELEGATE PlayPauseMusic:nil];
  3. }
  4. -(void) remoteStop {
  5. [APP_DELEGATE PlayPauseMusic:nil];
  6. }
  7. -(void) loadNextSong {
  8. [APP_DELEGATE next:nil];
  9. }
  10. -(void) loadPrevIoUsSong {
  11. [APP_DELEGATE prevIoUs:nil];
  12. }

猜你在找的iOS相关文章