AirPlay

作者: JustinYang | 来源:发表于2016-08-01 16:53 被阅读1183次

    title: AirPlay
    date: 2016-07-16
    tags: AirPlay,AirPlay设备自动选择
    博客地址

    AirPlay

    需求:绕过系统限制,自动选择支持AirPlay的设备

    Airplay基础知识

    要调用出AirPlay列表需要使用到MPVolumeView控件,当系统检测到网络环境中有支持AirPlay的设备时才会出现AirPlay图标,用户点击这个图标,呼出支持AirPlay的设备列表。
    MPVolumeView *volumView = [[MPVolumeView alloc] initWithFrame:CGRectMake(60, 100, 40, 40)];
    [volumView setRouteButtonImage:[UIImage imageNamed:@"pic-02"] forState:UIControlStateNormal];
    volumView.showsVolumeSlider = NO;
    setRouteButtonImage自定义AirPlay图标

    自动选择AirPlay设备

    自动选择AirPlay设备要使用了MediaPlayer.framework的私用类MPAVRoutingController,在这里可以搜到iOS所有的framework的头文件。

    Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
    self.routerController = [[MPAVRoutingController alloc] init];
    [self.routerController setValue:self forKey:@"delegate"];
    [self.routerController setValue:[NSNumber numberWithLong:2] forKey:@"discoveryMode"];
    

    NSClassFromString获取MPAVRoutingController类定义,并通过KVC给它的属性赋值,discoveryMode为2时才能及时在AirPlay设备发生变化时触发它协议的-(void)routingControllerAvailableRoutesDidChange:(id)arg1;方法

    -(void)routingControllerAvailableRoutesDidChange:(id)arg1{
        if (self.deviceName == nil) {
            return;
        }
        NSArray *availableRoutes = [self.routerController valueForKey:@"availableRoutes"];
        for (id router in availableRoutes) {
            NSString *routerName = [router valueForKey:@"routeName"];
            if ([routerName rangeOfString:self.deviceName].length >0) {
                BOOL picked = [[router valueForKey:@"picked"] boolValue];
                if (picked == NO) {
                    [self.routerController performSelector:@selector(pickRoute:) withObject:router];
                }
                return;
            }
        }
    }
    

    MPAVRoutingControllerDelegateroutingControllerAvailableRoutesDidChange用代码来选择用户指定的AirPlay设备,如果用户想取消自动选择的功能,将deviceName属性赋值为nil即可。

    相关文章

      网友评论

      • TonyDuan:能在iOS11上面实现mirror功能吗?我在iOS10上面可以.
      • Lius_:@JustinYang 使用私有类不会被拒吗?
        JustinYang:上次加在我们的app中 ,没被拒绝,怕被拒的话,把类名弄成ASCII ,我拉我有人这样用私有api 来防止被拒
      • meng_huang:可不可以写入一个AirPlay设备信息
      • leimeimei:你好,使用你的代码总是报错,找不到类,这些私有的类和代理要怎么用呢?能给个demo吗?新手求指教
        JustinYang:你运行时时候,保证所处的网络有支持AirPlay的设备,然后才能看到图标, self.airplayPicker.deviceName = @"X3";这个赋值成你想要自动选择的AirPlay设备的名字,就可以了
        JustinYang:这是demo地址 https://github.com/JustinYangJing/AirPlayTest.git

      本文标题:AirPlay

      本文链接:https://www.haomeiwen.com/subject/dhzxsttx.html