简体   繁体   English

带Cordova插件的iOS上的remoteControlReceivedWithEvent

[英]remoteControlReceivedWithEvent on iOS with cordova plugin

I have a audio player app, that is created with Cordova and native AudioStreamer plugin calls.. Everything works perfectly, BUT, now i want to use the remoteControlReceivedWithEvent event to use the native remote controle when the app is in the background.. 我有一个音频播放器应用程序,它是用Cordova和本机AudioStreamer插件调用创建的。.一切正常,但是,现在我想使用remoteControlReceivedWithEvent事件在应用程序在后台时使用本机远程控件。

When I call my Cordova plugin to start the native player I also call .. 当我致电Cordova插件启动本机播放器时,我也致电..

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

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

And when I stop the stream: 当我停止播放时:

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

   [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 

It all work perfect, but i DONT know where to put the remote events... 一切都很完美,但是我不知道在哪里放置远程事件...

- (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;
}

} }

"[self canBecomeFirstResponder];" “ [self canBecomeFirstResponder];” can not work because this method is for UIResponder and CDVPlugin extend from NSObject. 无法使用,因为此方法适用于UIResponder并且CDVPlugin是从NSObject扩展的。

For this override pluginInitialize method like bellow: 对于此覆盖的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];
}

Noticed, MainViewController is first responder so it will take all remote event. 注意,MainViewController是第一响应者,因此它将处理所有远程事件。 Now add property in MainViewController.h then controller can pass to the desired plugin 现在在MainViewController.h中添加属性,然后控制器可以传递给所需的插件

@property (nonatomic, weak) CDVPlugin *remoteControlPlugin;

And add remote event method like that which call your remote plugin method 并添加调用您的远程插件方法的远程事件方法

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

Now put remoteControlReceivedWithEvent in your plugin too. 现在也将remoteControlReceivedWithEvent放入您的插件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM