AVFoundation使用AirPlay

我们知道AirPlay可以直接使用控制中心去调.然而这样严格的来说不能算是将播放的内容投放到AirPlay端.倒是直接投屏了.

这里我们拿AVPlayer举例子.

self.player = [[AVPlayer alloc] init];
self.player.allowsExternalPlayback = YES;
self.player.usesExternalPlaybackWhileExternalScreenIsActive = YES;

allowsExternalPlayback设置为YES会允许播放器切换到外部播放模式(默认为YES).

usesExternalPlaybackWhileExternalScreenIsActive外部屏幕处于活动状态的时候是否自动切换到外部播放模式,默认为NO.如果上一个属性是NO,这个属性值就无效.

iOS 11以上

使用AVKit框架下的AVRoutePickerView类就行.可以想象成一个按钮,或者是UILabel.他有自己的大小.个人建议设置它的x和y的约束即可.

self.routePickerView = [[AVRoutePickerView alloc] init];
self.routePickerView.delegate = self;
[self.view addSubview:self.routePickerView];
self.routePickerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.routePickerView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[self.routePickerView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;

iOS 11以下

可以使用MeidaPlayer框架下的MPVolumeView(知名软件VLC用的就是这个).和上面的差不多,也就是多了个音量控制的slider.不过宽高也得指定这个很烦.

self.volumeView = [[MPVolumeView alloc] init];
self.volumeView.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:self.volumeView];
self.volumeView.translatesAutoresizingMaskIntoConstraints = NO;
[self.volumeView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[self.volumeView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
[self.volumeView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[self.volumeView.heightAnchor constraintEqualToConstant:44].active = YES;

需要注意的是.AriPlay的那个图标是个白色的.如果不社会背景色有可能看不出来.不管有没有支持AirPlay的设备.这个图标都会显示.

发布了268 篇原创文章 · 获赞 59 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/99684228
今日推荐