2013-06-19 91 views
1

我有一個音頻播放器應用程序,使用Cordova和本機AudioStreamer插件調用創建。一切正常,但是,現在我想要使用remoteControlReceivedWithEvent事件來使用本地遠程控制。應用程序在後臺..在iOS上使用cordova插件的remoteControlReceivedWithEvent

當我打電話給我的科爾多瓦插件啓動本地球員,我也呼籲..

- (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在你的插件了。

相關問題