我正在开发一个带有标签栏控制器的 iOS 应用程序。在第一个选项卡中,我放置了一个 AVQueuePlayer 实例来开始从网络播放音乐。我完成了所有编码以允许通过远程控制事件播放和暂停事件。但只有当我停留在第一个选项卡中时,我才能接收远程控制事件。当我切换到其他选项卡时,第一个选项卡不会收到远程控制事件。
当我将以下行放在第一个选项卡视图控制器中时,即使我停留在第二个选项卡中,我也可以接收第一个选项卡的远程控制事件。
- (BOOL)canResignFirstResponder
{
return NO;
}
但我在其他视图中有一些文本字段,用户必须与之交互。通过不放弃第一个选项卡中的第一响应者,我无法在其他选项卡中输入文本。
请帮助我,当我的用户与第二个选项卡中的应用程序交互时,如何处理远程控制事件以控制第一个选项卡中的 AVQueuePlayer 实例?
感谢您的帮助 !
好的。我自己想出来的。
我在实现文件的开头为 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
对于苹果文档中规定的处理远程控制事件的所有其他方法,请参阅:
iOS 事件处理指南 - 多媒体远程控制 http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)