Receiving RemoteControlEvents to First Tab when interacting in Second Tab in a TabBar Controlled iOS App

孤人 提交于 2019-12-12 14:47:02

问题


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:


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



来源:https://stackoverflow.com/questions/10940201/receiving-remotecontrolevents-to-first-tab-when-interacting-in-second-tab-in-a-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!