当前位置: 首页 > 工具软件 > AirPlay > 使用案例 >

ios10 Android airplay,iOS下AirPlay投屏功能实现

王亮
2023-12-01

这篇文章注重于如何实现AirPlay投屏功能.具体AirPlay的实现逻辑这里不再赘述,网上帖子很多

首先.想要呼出AirPlay列表的话,需要将MPVolumnView控件声明且添加到UI.上使用之前需要引入头文件.后续如果有AirPlay设备可用并且MPVolumnView存在于UI中的话.即可呼出列表

MPVolumnView:一个系统内置的控件.继承自UIView.内部自定义了三个控件:MPVolumeSlider(音量进度条)UILabel(显示文字)MPButton(点击呼出AirPlay选择器)

①如何解决MPVolumnView添加到UI中之后高亮效果会出现系统原生icon的问题

答:设置不同State的image为ni即可.

code:

MPVolumnView *volumnView = [[MPVolumnView alloc] init];

//此处btnSender的实际类型为MPButton

UIButton *btnSender = [volumnView objectWithBlock:^BOOL(UIView *obj) {

return [obj isKindOfClass:[UIButton class]];

}];

[btnSender setImage:nil forState:UIControlStateNormal];

[btnSender setImage:nil forState:UIControlStateHighlighted];

[btnSender setImage:nil forState:UIControlStateSelected];

②如何解决在没有可用设备情况下点击MPVolumnView不会呼出AudioRoutePicker的问题

答:目前我找到的一个解决方案是完全无视MPVolumnView.将它在init时frame设置为0,0,0,0 然后在界面上自己添加一个UIButton.在它的点击事件中给MPVolumnView的MPButton发送一个UIControlEventTouchUpInside来触发MPVolumnView的_displayAudioRoutePicker事件

code:

//首先.自定义类.并且继承MPVolumnView

@interface SparkMPVolumnView : MPVolumeView

@property(nonatomic,weak)UIButton *MPButton;

@end

@implementation SparkMPVolumnView

-(instancetype)init{

if(self = [self initWithFrame:CGRectZero]){

self.backgroundColor = [UIColor clearColor];

self.showsVolumeSlider = NO;

self.tag = kTVMPVolumeViewTag;

[self initMPButton];

}

return self;

}

//这个的目的是在AirPlay没有任何设备时也能呼出Picker使用

- (void)initMPButton{

UIButton *btnSender = [self.subviews objectWithBlock:^BOOL(UIView *obj) {

return [obj isKindOfClass:[UIButton class]];

}];

[btnSender setImage:nil forState:UIControlStateNormal];

[btnSender setImage:nil forState:UIControlStateHighlighted];

[btnSender setImage:nil forState:UIControlStateSelected];

[btnSender setBounds:CGRectZero];

self.MPButton = btnSender;

}

//在自定义按钮的按下事件中发送给MPButton

//此处sparkVolumView为一个SparkMPVolumnView实例

-(void)airPlayButtonAction:(UIButton *)sender{

[self.sparkVolumView.MPButton sendActionsForControlEvents:UIControlEventTouchUpInside];

}

③如何知道AirPlay的连接和断开状态?

答:其他资料大多是通过注册MPVolumeViewWirelessRouteActiveDidChangeNotification来进行判断的.但是这个通知有一个问题就是在存在多个MPVolumnView时会出现多次发送,并且如果在声明MPVolumnView控件时是连接AirPlay设备的,它会先发送一个属性wirelessRouteActive为NO的通知(即未连接任何设备),然后立马发送一个wirelessRouteActive位YES的通知(连接到了设备).故我并未采用此方案.

我注册了AVAudioSessionRouteChangeNotification通知.这个通知在音频通道发生变化时会进行调用(例如插入/拔出耳机 扬声器/听筒切换 连接/断开AirPlay设备).这个的通知是唯一且不重复的.在发生改变时[AVAudioSession sharedInstance]会发送此通知.在具体通知中通过获取当前的AirPlay通道名称来判断是否连接到了投屏设备

code:

-(void)registerNotification{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteHasChangedNotification:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

}

- (void)audioRouteHasChangedNotification:(NSNotification *)notification{

NSString *airplayDeviceName = [self activeAirplayOutputRouteName];

BOOL isAirPlay = self.airplayDeviceName.length > 0;

}

//遍历当前设备所有通道.返回isEqualToString:AVAudioSessionPortAirPlay通道的具体名称,如果名称不为nil则为当前连接到了AirPlay

- (NSString*)activeAirplayOutputRouteName

{

AVAudioSession* audioSession = [AVAudioSession sharedInstance];

AVAudioSessionRouteDescription* currentRoute = audioSession.currentRoute;

for (AVAudioSessionPortDescription* outputPort in currentRoute.outputs){

if ([outputPort.portType isEqualToString:AVAudioSessionPortAirPlay])

return outputPort.portName;

}

return nil;

}

④为什么播放过程中会电视端会出现暂停和退出播放的情况

答:AirPlay实现的原理是一个Socket通信.并且会在新的通道请求之后断开之前的.所有在项目中你理论上是有多处会播放视频的.只要调用一个新播放器的Play方法.设备端就会认为你重新发起了一个Socket请求,断开之前的播放并且发起新的视频播放.故需要保证在AirPlay连接的情况下不调用任何视频的暂停和播放代码.

在我的项目中逻辑是比较复杂的,故实现方式是通过一个单例的Manager强引用一个AVPlayer,所有的AirPlay播放和暂停请求都通过它来实现..其他代码只要保证在连接AirPlay的时候不进行操作就可以了.

⑤AirPlay在播放时播放新视频为什么可能会投屏失败?

答:这个我也无解.没有查到相关的资料.现在的解决方案是在投放新视频前检测,如果只有有视频,暂停播放并且将AVPlayer设置为nil.保证电视端退出投屏,随后增加1.5f延时再进行播放.基本解决了投屏失败的问题

其他问题欢迎大家在评论区讨论,做AirPlay时趟了许多坑.希望这篇文章能帮助大家少走弯路.有不对的地方希望指出,第一时间修正

 类似资料: