3

我正在开发带有选项卡栏控制器的iOS应用程序。在第一个标签中,我放置了一个AVQueuePlayer的实例来开始从网络播放音乐。我做了所有的编码,以允许通过远程控制事件播放和暂停事件。但是,只有当我留在第一个标签中时,我才能够接收远程控制事件。当我切换到其他选项卡时,远程控制事件不会收到第一个选项卡。当在TabBar受控iOS应用程序的第二个选项卡中进行交互时,将RemoteControlEvents接收到第一个选项卡

当我在第一个选项卡视图控制器中放置以下行时,即使我留在第二个选项卡中,我也可以将遥控事件接收到第一个选项卡。

- (BOOL)canResignFirstResponder 
{ 
    return NO; 
} 

但是我在其他视图中有一些用户必须与之交互的文本字段。通过不退出第一个选项卡中的第一响应者,我无法在其他选项卡中输入文本。

请帮助我如何处理远程控制事件以控制第一个选项卡中的AVQueuePlayer实例,同时我的用户在第二个选项卡中与应用程序交互?

感谢您的帮助!

+0

谢谢您的问题!切换到第二个标签让我疯狂。我没有任何文本字段,所以对我来说更容易=) – Dimme

回答

0

好的。我自己想到了。

我在开始的实现文件中为avqueueplayer创建了一个全局变量。 在viewDidLoad方法中分配并启动了AVQueuePlayer。 创建一个类方法来处理播放和暂停事件。 并在其他视图控制器中调用此类方法以直接从这些视图控制器处理远程控制事件。这里是什么,我编写了一个样本:

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

对于所有其他的方法来处理为苹果奠定了文档中的远程控制事件是指:

Event Handling Guide for iOS - Remote Control of Multimedia

相关问题