2013-06-19 61 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在你的插件了。

相关问题