I am developing an iOS app with a tab bar controller. In the first tab, I placed an instance of AVQueuePlayer to start playing music from the web. I did all coding to allow play and pause events through remote control events. But I could able to receive remote control events only when I stay in the first tab. When I switch to other tabs, remote control events are not received to the first tab.

When I place the following lines in first tab view controller, I can receive remote control events to first tab even when I stay in second tab.

- (BOOL)canResignFirstResponder
{
    return NO;
}

But I have some text fields in other views with which the user has to interact. By not resigning first responder in first tab, I cannot input text in other tabs.

Please help me how can I handle remote control events to control an AVQueuePlayer instance in first tab while my user interacts with the app in second tab ?

Thanks for your help !

1

There are 1 answers

0
Prasi On BEST ANSWER

OK. I figured it myself.

I created a global variable for the avqueueplayer in the starting of the implementation file. Allocated and initiated the AVQueuePlayer in viewDidLoad method. Created a class method to handle what to do in the event of play and pause. And called this class method in other view controllers to handle the remote control events directly from those view controllers. Here is a sample of what I coded:

//playerView header file

@interface playerView : UIViewController

+ (void)togglePlayPause;

@end

//playerView Implementation File

#import "playerView.h"

@interface playerView ()
@end

@implementation playerView

AVQueuePlayer *player;

- (void)viewDidLoad
{
[super viewDidLoad];
player = [[AVQueuePlayer alloc] initWithPlayerItem:[AVPlayerItem playerItemWithURL: someurl]];
}

+ (void) togglePlayPause
{
    if (player.rate == 1.0)
    {
        [player pause];
    }
    else if ((player.rate == 0.0) && ([player status]!= 2))
    {
        [player play];
    }
}

// include all other methods to handle remote control events as laid in apple documentation

@end



//otherView Implementation file

#include "playerView.h"


@interface otherView ()

@end

@implementation otherView

// include all other methods to handle remote control events as laid in apple documentation

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent
{

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [playerView togglePlayPause];
                break;
            default:
                break;
        }
    }
}

@end

For all other methods to handle remote control events as laid in apple documentation refer to:

Event Handling Guide for iOS - Remote Control of Multimedia