2013-06-19 12 views
1

У меня есть приложение для аудиоплеера, которое создается с помощью плагинов Cordova и родных подключаемых модулей AudioStreamer .. Все работает отлично, НО, теперь я хочу использовать событие remoteControlReceivedWithEvent для использования собственного удаленного управления, когда приложение работает в фоновом режиме ..remoteControlReceivedWithEvent на iOS с плагином cordova

когда я звоню мой Кордова плагин для запуска родной плеер я также называю ..

- (void)startStream:(CDVInvokedUrlCommand*)command 
    streamer = [[[AudioStreamer alloc] initWithURL:url] retain]; 
    [streamer start]; 

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    [self canBecomeFirstResponder]; 

И когда я остановить поток:

- (void)stopStream:(CDVInvokedUrlCommand*)command 
    [streamer stop]; 
    [streamer release]; 
    streamer = nil; 

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 

Это все работает идеально, но я не знаю, куда девать отдаленные события ...

- (void)remoteControlReceivedWithEvent:(UIEvent *)event { 
    switch (event.subtype) { 
       case UIEventSubtypeRemoteControlTogglePlayPause: 
       NSLog(@"PAUSE!!!"); 
       break; 

       case UIEventSubtypeRemoteControlPlay: 
       NSLog(@"PAUSE!!!"); 
     break; 
       case UIEventSubtypeRemoteControlPause: 
         NSLog(@"PAUSE!!!"); 
         break; 
       case UIEventSubtypeRemoteControlStop: 
         NSLog(@"PAUSE!!!"); 
         break; 
       default: 
       break; 
} 

}

ответ

0

"[само canBecomeFirstResponder];" не может работать, потому что этот метод для UIResponder и CDVPlugin простирается от NSObject.

Для этого переопределения метода pluginInitialize как ниже:

- (void)pluginInitialize 
{ 
    [super pluginInitialize]; 
    [[AVAudioSession sharedInstance] setDelegate:self]; 

    NSError *error = nil; 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setMode:AVAudioSessionModeDefault error:&error]; 
    if (error) 
     [[[UIAlertView alloc] initWithTitle:@"Audio Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 

    error = nil; 
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error]; 
    if (error) 
     [[[UIAlertView alloc] initWithTitle:@"Audio Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 


    MainViewController *mainController = (MainViewController*)self.viewController; 
    mainController.remoteControlPlugin = self; 
    [mainController canBecomeFirstResponder]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
} 

ЗАМЕТИЛИ, MainViewController является первым ответчиком, так что будет принимать все удаленные события. Теперь добавьте свойство в MainViewController.h тогда контроллер может перейти к нужному плагин

@property (nonatomic, weak) CDVPlugin *remoteControlPlugin; 

И добавить метод удаленного события, как то, что называют ваш удаленный метод плагин

- (void)remoteControlReceivedWithEvent:(UIEvent*)event 
{ 
    if ([self.remoteControlPlugin respondsToSelector:@selector(remoteControlReceivedWithEvent:)]) 
     [self.remoteControlPlugin performSelector:@selector(remoteControlReceivedWithEvent:) withObject:event]; 
} 

Теперь поместите remoteControlReceivedWithEvent в плагине тоже.