简体   繁体   中英

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..

When I call my Cordova plugin to start the native player I also call ..

- (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];" can not work because this method is for UIResponder and CDVPlugin extend from NSObject.

For this override pluginInitialize method like bellow:

- (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. Now add property in MainViewController.h then controller can pass to the desired plugin

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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