gpt4 book ai didi

ios - MPVolumeView Airplay 仅在镜像时触摸

转载 作者:行者123 更新时间:2023-11-28 06:12:53 30 4
gpt4 key购买 nike

我正在使用自定义 AVPlayerLayer 来显示一个简单的视频。我正在尝试添加 airplay 支持,但是点击该按钮时没有显示任何内容。

self.player.allowsExternalPlayback = true
...
let airplayButton = MPVolumeView(frame: self.airplayButtonPlaceholder!.bounds)
airplayButton.showsRouteButton = true
airplayButton.showsVolumeSlider = false

self.airplayButtonPlaceholder?.addSubview(airplayButton)
self.airplayButtonPlaceholder?.backgroundColor = UIColor.clear

当我运行我的代码(在真实设备上)时,我看到了按钮,但是当我点击它时,没有任何反应。是什么原因造成的?是因为我使用的是自定义 AVPlayerLayerAVPlayer 吗?

编辑:

当我通过控制中心打开镜像时,我可以触摸按钮并显示弹出窗口。怎么回事?

最佳答案

没有任何反应,因为您没有正确配置这个“新窗口”。

有两种使用 Airplay 显示内容的方法。

镜像

不需要任何配置。

Note: You don’t need to do anything to make mirroring happen. In iOS 5.0 and later, mirroring—that is, displaying the same content on both the host device and the external display—occurs by default when the user selects an AirPlay video output device.

额外窗口

( check apple guide here )

苹果描述的步骤是:

  1. 在应用启动时,检查是否存在外部显示器并注册屏幕连接和断开连接通知。
  2. 当外部显示器可用时——无论是在应用启动时还是您的应用正在运行时——为其创建并配置一个窗口。
  3. 将窗口与适当的屏幕对象相关联,显示第二个窗口,并正常更新它。

这是从苹果文档中获取的代码,供快速引用。


- 如果外部显示器已经存在,则创建一个新窗口

- (void)checkForExistingScreenAndInitializeIfPresent
{
if ([[UIScreen screens] count] > 1)
{
// Get the screen object that represents the external display.
UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
// Get the screen's bounds so that you can create a window of the correct size.
CGRect screenBounds = secondScreen.bounds;

self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
self.secondWindow.screen = secondScreen;

// Set up initial content to display...
// Show the window.
self.secondWindow.hidden = NO;
}
}

- 注册连接和断开连接通知

- (void)setUpScreenConnectionNotificationHandlers
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
name:UIScreenDidDisconnectNotification object:nil];
}

- 处理连接和断开连接通知

- (void)handleScreenDidConnectNotification:(NSNotification*)aNotification
{
UIScreen *newScreen = [aNotification object];
CGRect screenBounds = newScreen.bounds;

if (!self.secondWindow)
{
self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
self.secondWindow.screen = newScreen;

// Set the initial UI for the window.
}
}

- (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification
{
if (self.secondWindow)
{
// Hide and then delete the window.
self.secondWindow.hidden = YES;
self.secondWindow = nil;

}

}

编辑:

当使用 AVPlayerViewController 时,它已经按照文档中的描述自动实现 here .

AVPlayerViewController automatically supports AirPlay, but you need to perform some project and audio session configuration before it can be enabled in your application.

关于ios - MPVolumeView Airplay 仅在镜像时触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010067/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com