Ich versuche, Fernbedienungsereignisse zu einer Musik-Player-App hinzuzufügen, aber es funktioniert nicht.iOS MediaPlayer Fernbedienungsereignisse funktionieren nicht
Einige Fakten:
- Ich
[MPMusicPlayerController applicationMusicPlayer];
- mit I Audio, und Bild in Bild Hintergrund Modi AirPlay hinzugefügt;
- Das Beispiel spielt Audio;
- Es werden keine Remote-Benachrichtigungen vom Sperrbildschirm und vom Kontrollzentrum empfangen.
Ich habe ein Beispielprojekt gemacht, die hier heruntergeladen werden kann: example project Die Hauptquelle:
#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;
}
Ich kann einfach nicht herausfinden, warum es :(
Arbeits nichtPROGRESS UPDATE'S Was ich bisher herausgefunden habe, wenn ich mein iPhone neu starte, funktioniert es, aber jedes Mal danach wird es nicht.
Der Sperrbildschirm und das Kontrollzentrum zeigen beide den richtigen Track an, können aber die App nicht steuern.
Haben Sie das jemals zur Arbeit gebracht? Ich habe das letzte Jahr oder so aufgegeben. Ich hoffe, dass jemand es herausgefunden hat. Wenn Sie es nicht herausgefunden haben, würde ich das auch gerne wissen. Vielen Dank. – JeffB6688