带有cordova插件的iOS上的remoteControlReceivedWithEvent

时间:2013-06-19 06:53:07

标签: ios cordova audiostreamer

我有一个音频播放器应用程序,用Cordova和本机AudioStreamer插件调用创建..一切都很完美,但是,现在我想使用remoteControlReceivedWithEvent事件在应用程序在后台时使用本机远程控制。

当我打电话给我的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;
}

}

1 个答案:

答案 0 :(得分:0)

" [self 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放入你的插件中。