2016-06-29 3 views
3

음악 플레이어 앱에 원격 제어 이벤트를 추가하려하지만 작동하지 않습니다.iOS MediaPlayer 원격 제어 이벤트가 작동하지 않습니다.

몇 가지 사실 :

  • 내가 사진 배경 모드에서 오디오, AirPlay를사진을 추가
  • [MPMusicPlayerController applicationMusicPlayer];를 사용하고 있습니다;
  • 이 예에서는 오디오를 재생 중입니다.
  • 잠금 화면과 제어 센터에서 원격 알림을받지 못합니다.

나는 여기에서 다운로드 할 수있는 예제 프로젝트를 만들었어요 : 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을 재부팅하면 작동하지만 그 후에는 그렇지 않을 때입니다.

잠금 화면과 제어 센터가 모두 올바른 트랙을 표시하지만 여전히 앱을 제어 할 수 없습니다.

+0

작동하도록 설정 했습니까? 나는 지난 1 년 정도를 포기했다. 나는 누군가가 그것을 알아 냈 으면한다. 당신이 그것을 알아 내지 못했다면 나는 그것을 알고 싶습니다. 감사. – JeffB6688

답변

-1

이것이 도움이 될지 확실하지 않지만 여기에 변경 사항을받은 방법이 나와 있습니다 (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)이다. 아직 원격 제어 이벤트에 익숙하지 않지만 이러한 알림을 끝내기를 원할 수도 있습니다. 희망이 도움이!

관련 문제