问题描述
我正在从应用程序中播放Apple Music,Apple音乐播放器代码为-
I am playing Apple Music from my application , the apple music player code is as -
-(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
which is working great . Now I wants to control the track from lock screen, for this i did following code in viewWillAppear
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[APP_DELEGATE becomeFirstResponder];
和remoteControlReceivedWithEvent
方法被写入AppDelegate
文件中,如下所示-
and remoteControlReceivedWithEvent
method is written in AppDelegate
file as follows -
-(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
曲目或Spotify iOS SDK播放Spotify
曲目,则每次触发remoteControlReceivedWithEvent
.
Note - The remoteControlReceivedWithEvent
triggered every time if iTunes
tracks are playing by AVPlayer
OR Spotify
tracks are playing by Spotify iOS SDK .
但是,通过-播放Apple Music曲目时不会触发相同的代码.
But same code is not triggering while playing Apple Music tracks by -
[MPMusicPlayerController applicationMusicPlayer]
or
[MPMusicPlayerController systemMusicPlayer]
任何帮助将不胜感激.谢谢
any help will be appreciated. Thanks
推荐答案
不是使用-(void)remoteControlReceivedWithEvent:(UIEvent *)event
来跟踪控件,而是使用MPRemoteCommandCenter
将目标添加到每个控件中:
Instead of using -(void)remoteControlReceivedWithEvent:(UIEvent *)event
to track for the controls, use MPRemoteCommandCenter
adding targets to each one of the controls:
[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];
}
这篇关于beginReceivingRemoteControlEvents不会触发Apple Music的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!