我正在尝试将远程控制事件添加到音乐播放器应用程序,但无法正常运行。
一些事实:
- 我正在使用
[MPMusicPlayerController applicationMusicPlayer];
- 我在图片背景模式中添加了音频、AirPlay和图片;
- 例子是播放音频;
- 它不接收来自锁屏和控制中心的远程通知;
我做了一个示例项目,可以在这里下载: example project 主要来源:
#import "ViewController.h"
#import <MediaPlayer/MPMediaPickerController.h>
#import <MediaPlayer/MPMediaQuery.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (strong, nonatomic) MPMusicPlayerController *MusicPlayer;
@property (strong, nonatomic) MPMediaItemCollection *MusicPlayerSongs;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_MusicPlayerSongs = [MPMediaItemCollection alloc];
_MusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[_MusicPlayer setShuffleMode: MPMusicShuffleModeOff];
[_MusicPlayer setRepeatMode: MPMusicRepeatModeNone];
[_MusicPlayer beginGeneratingPlaybackNotifications];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSMutableArray *selectedTracks = [NSMutableArray new];
//Find all tracks that contains an 'a'
MPMediaPropertyPredicate *songPredicate =
[MPMediaPropertyPredicate predicateWithValue:@"a"
forProperty:MPMediaItemPropertyTitle
comparisonType:MPMediaPredicateComparisonContains];
MPMediaQuery *mediaQuery = [[MPMediaQuery alloc] init];
[mediaQuery addFilterPredicate:songPredicate];
[selectedTracks addObjectsFromArray:[mediaQuery items]];
NSLog(@"Number of tracks containing an 'a': %lu",(unsigned long)selectedTracks.count);
self.MusicPlayerSongs = [[MPMediaItemCollection alloc]initWithItems:selectedTracks];
[self.MusicPlayer setQueueWithItemCollection:self.MusicPlayerSongs];
[self.MusicPlayer play];
[self.MusicPlayer beginGeneratingPlaybackNotifications];
[self basicSetup];
[self updateNowPlayingCenter];
}
- (void)basicSetup {
//Listen to remote control events
[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = NO;
[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = NO;
[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(remoteNext)];
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(remotePrevious)];
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remotePlay)];
[[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(remoteTogglePlayState)];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
-(void)remotePlay{
NSLog(@"remotePlay");
[self.MusicPlayer play];
}
-(void)remoteNext{
NSLog(@"remoteNext");
[self.MusicPlayer skipToNextItem];
[self updateNowPlayingCenter];
}
-(void)remotePrevious{
NSLog(@"remotePrevious");
[self.MusicPlayer skipToPreviousItem];
[self updateNowPlayingCenter];
}
-(void)remotePause{
NSLog(@"remotePause");
[self.MusicPlayer pause];
}
-(void)remoteTogglePlayState{
NSLog(@"remoteTogglePlayState");
if([self.MusicPlayer playbackState] == MPMusicPlaybackStatePlaying){
[self.MusicPlayer pause];
}else{
[self.MusicPlayer play];
}
}
-(void)updateNowPlayingCenter{
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *songInfo = [NSMutableDictionary dictionaryWithDictionary:@{
MPMediaItemPropertyArtist: [self.MusicPlayer nowPlayingItem].artist,
MPMediaItemPropertyTitle: [self.MusicPlayer nowPlayingItem].title,
MPMediaItemPropertyAlbumTitle: [self.MusicPlayer nowPlayingItem].albumTitle,
}];
center.nowPlayingInfo = songInfo;
}
我只是想不通为什么它不起作用:(
进度更新 到目前为止我发现,如果我重新启动我的 iPhone,它就可以工作。但之后每次都不会。
锁屏和控制中心都显示正确的轨道,但仍然无法控制应用。
最佳答案
不确定这是否对您有帮助,但这是我收到任何更改的方式(在 Swift 中)。
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.getNowPlayingItem), name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
player.beginGeneratingPlaybackNotifications()
}
deinit {
player.endGeneratingPlaybackNotifications()
NSNotificationCenter.defaultCenter().removeObserver(self)
}
有必要(addObserver 和 beginGeneratingPlaybackNotifications),也有必要(removeObserver 和 endGeneratingPlaybackNotification)。我还不熟悉远程控制事件,但您可能也想尝试结束这些通知。希望这对您有所帮助!
关于iOS MediaPlayer 远程控制事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096793/