ios - 使用 AVPlayer 获取 AirPlay 设备的名称

标签 ios avplayer

当在 MPMoviePlayerController 中启用 AirPlay 时,它会显示文本“此视频正在设备名称 上播放”。将 AirPlay 与 AVPlayer 一起使用时,是否可以通过编程方式获取设备名称?

最佳答案

从 iOS7 开始,currentRoute 的 AudioToolbox API 已弃用:

Apple 改为在 AudioSession 中为您提供 currentRoute API,这允许检索它的端口信息以及以一种很好的方式收听 audioRouteChangeNotification:

NSString* airplayName = [self activeAirplayOutputRouteName];
if (airplayName) {
    //airplay is active

}

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

(你要获取的是audioSession.currentRouteportDescriptionportType):

- (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;
}

- (void)audioRouteHasChangedNotification:(NSNotification*)notification
{
    //do something
}

关于ios - 使用 AVPlayer 获取 AirPlay 设备的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044894/

相关文章:

ios - 从数据 block 播放视频

iphone - AVPlayer 设置初始播放时间 iOS

ios - 在 AVPlayer 中显示播放控件

ios - 不明白为什么我会收到 Parse 206 错误 (UserCannotBeAlteredWithoutSessionError)

ios - main.jsbundle 文件显示在我的 iOS 项目中,但仍然抛出 "No bundle url present"

ios - 使用 ARC,是否需要释放 CGMutablePath Refs?

ios - iOS 12 的 AVPlayerViewController 问题

ios - 我可以在 UITableView 中添加带有虚拟数据的临时单元格吗?

ios - NSURLCredentialStorage 默认凭证不会自动使用

swift - 从 MP3 HTTP 流中获取元数据