Ama*_*dir 7 cocoa-touch objective-c ios avplayer airplay
我正在使用Apple Airplay并AVPlayer在外部显示器上显示视频,同时使用Apple TV和AirPlay在设备上显示一些控件.
我正在使用这个类AVPlayer在外部屏幕上显示:
#import "AirPlayViewController.h"
@interface AirPlayViewController ()
@end
@implementation AirPlayViewController
- (id)initWithFrame:(CGRect)iFrame{
self.view.frame = iFrame;
self.backGroundImageView = [[UIImageView alloc]initWithFrame:iFrame];
[self.view addSubview: self.backGroundImageView];
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
}
-(void)viewWillDisappear:(BOOL)animated{
[audioVideoPlayer pause];
}
-(void)playMediaWithURL:(NSURL*)aURL{
audioVideoPlayer = [AVPlayer playerWithURL:aURL];
audioVideoPlayer.usesExternalPlaybackWhileExternalScreenIsActive=YES;
audioVideoPlayer.allowsExternalPlayback= YES;
//manually resizing and adding the layer, so that the app does not get confused with airplayfunctionality
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer: audioVideoPlayer];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
if (audioVideoPlayer) {
[audioVideoPlayer play];
}
}
-(void)stopMediaPlayback{
[audioVideoPlayer pause];
[[AVPlayerLayer playerLayerWithPlayer: audioVideoPlayer] removeFromSuperlayer];
audioVideoPlayer = nil;
}
@end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题就在那里playMediaWithURL:.该AVPlayer图层不会自动添加到外部屏幕,但我必须手动添加它,感觉有点脏,当我想AVPlayer再次从视图中删除时,会导致所有类型的清理工作.
有没有其他人遇到这个问题,并找到一些巧妙的方法来实现它?
在airPlayViewController从另一个叫做UIViewController包含第二个窗口以及所有的AirPlay的委托东西来显示它,使用此功能:
- (void)setupMirroringForScreen:(UIScreen *)anExternalScreen{
//find the correct resolution for the external screen
UIScreen *tScreen= anExternalScreen;
CGSize max = {0, 0};
UIScreenMode *maxScreenMode = nil;
for (UIScreenMode *current in tScreen.availableModes) {
if (maxScreenMode == nil || current.size.height > max.height || current.size.width > max.width) {
max = current.size;
maxScreenMode = current;
}
}
tScreen.currentMode = maxScreenMode;
// Setup window in external screen
self.externalWindow = [[UIWindow alloc] initWithFrame:tScreen.bounds];
self.externalWindow.hidden = NO;
self.externalWindow.layer.contentsGravity = kCAGravityResizeAspect;
self.externalWindow.screen = tScreen;
self.airPlayVC = [[AirPlayViewController alloc]initWithFrame:tScreen.bounds];
[self.airPlayVC.backGroundImageView setImage:[UIImage imageNamed:mainMenuExternalBackgroundImage]];
//try to play a video on the external Screen/ Audio file as fallback
NSURL *tURL= [[NSBundle mainBundle]URLForResource:mainMenuVideo withExtension:nil];
if (!tURL) {
tURL = [[NSBundle mainBundle] URLForResource:mainMenuAmbienceMusic withExtension:nil];
}
[self.airPlayVC playMediaWithURL:tURL];
[self.externalWindow addSubview:self.airPlayVC.view];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3076 次 |
| 最近记录: |