我的iPad应用程序通过嵌入式AVplayerViewController播放视频。当我将应用程序镜像到Apple TV时,视频仅出现在电视上并占据整个屏幕。嵌入式播放器显示以下消息:" TV Connected。这个视频正在电视上播放。
Apple文档对此行为保持沉默,除了声称视频应在镜像期间在两个设备上播放。和AVPlayer设置,例如' allowExternalPlayback = NO'没有效果。
如何在使用AVPlayer播放嵌入视频时保留应用镜像?
#import <AVKit/AVKit.h>
#import "myVC.h"
#import "VideoPlayer.h"
// video handlers
@property (weak, nonatomic) IBOutlet UIView *videoView;
@property IBOutlet AVPlayerViewController *videoVC;
@end
@implementation myVC
- (void)viewDidLoad {
[super viewDidLoad];
[self.videoView addSubview: videoVC.view];
[VideoPlayer loadVideo:videoUrl inVC:self.videoVC];
}
// video methods in custom VideoPlayer class
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface VideoPlayer : NSObject
@property (nonatomic) AVPlayer *player;
@property (nonatomic) AVPlayerViewController *videoVC;
+(void)loadVideo:(NSString*)name inVC:(AVPlayerViewController*)videoVC;
#import "VideoPlayer.h"
@implementation VideoPlayer
+(void)loadVideoURL: inVC:(AVPlayerViewController*)videoVC;
{
[self playVideo:url inVC:videoVC];
}
+(void)playVideo:(NSURL*)videoFileUrl inVC:(AVPlayerViewController*)videoVC;
{
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoFileUrl];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
videoVC.player = player;
[player play];
}