1

我有一个音频播放器应用程序,它是使用 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;
}

}

4

1 回答 1

0

“[自己可以成为第一响应者];” 无法工作,因为此方法适用于 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 也放入您的插件中。

于 2014-03-20T11:15:26.373 回答