如何使用 iOS 5+ AirPlay 用于第二个屏幕
我正在使用 iPhone 4S 尝试 AirPlay,但遇到了一些问题。我已将 Apple TV 2 更新到最新版本(我想是 4.4.3?我忘了,它在另一个房间,我懒得检查)并且我的 4S 上有 iOS 5.0,而我使用 Xcode 4.2 Build 4D199 。我编写了一个简单的 UIView 应用程序,尝试在第二个屏幕可用时进行绘制。这是我的 viewDidLoad 中的内容:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]];
NSLog(@"Registering for screen connect events...");
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(screenDidConnect:)
name:UIScreenDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(screenDidDisconnect:)
name:UIScreenDidDisconnectNotification
object:nil]; NSLog(@"There are %i connected screens.", ScreenCount);
if (ScreenCount > 1) {
self.secondWindow = [self myScreenInit:[[UIScreen screens] objectAtIndex:1]];
[self.secondWindow addSubview:[self createFullscreenViewForText:@"Second window" withSize:CGSizeMake(640, 480)]];
}
}
当我启用 TV Out 时,此代码可以在模拟器中运行,但如果我在调试时启用 TV Out,它总是会崩溃。在模拟器的硬件菜单中启用电视输出后,我必须重新启动应用程序。重新启动时,我在第二个屏幕上看到第二个屏幕 UILabel。在 iPhone 4S 上,只有使用运行任务栏中的 AirPlay 控件一直向左启用 AirPlay,才能实现此功能。您会注意到,我已将 MPVolumeView 组件添加到启动时的视图中,我尝试使用该组件连接到 Apple TV,但没有成功。
如果我使用应用程序内的 MPVolumeView,我可以成功地与 Apple TV 断开连接,但是当我再次使用它并选择 Apple TV 重新连接时,我不会收到 UIScreenDidConnectNotification。同样,我唯一能看到第二个屏幕的时间是启用镜像时。如果我连接任一 AirPlay 控件,我只计算 1 个屏幕,并且永远不会收到通知。另外,每次启用镜像时,我都会在控制台日志中看到以下内容:
Could not find mapped image ColorOnGrayShadow_AirPlay.png
这似乎已损坏,我想知道其他人在这方面的经验。当我学到新东西时,我将继续尝试并更新这篇文章。
I'm toying with AirPlay using an iPhone 4S and I'm having some trouble. I've updated my Apple TV 2 to the latest (4.4.3 I think? I forget, it's in the other room and I'm too lazy to check) and I have iOS 5.0 on my 4S while I use Xcode 4.2 Build 4D199. I wrote a simple UIView app that attempts to draw on a second screen when available. Here's what I have in my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]];
NSLog(@"Registering for screen connect events...");
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(screenDidConnect:)
name:UIScreenDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(screenDidDisconnect:)
name:UIScreenDidDisconnectNotification
object:nil]; NSLog(@"There are %i connected screens.", ScreenCount);
if (ScreenCount > 1) {
self.secondWindow = [self myScreenInit:[[UIScreen screens] objectAtIndex:1]];
[self.secondWindow addSubview:[self createFullscreenViewForText:@"Second window" withSize:CGSizeMake(640, 480)]];
}
}
This code works in the simulator when I enable TV Out although it always crashes if I enable TV Out while in debug. I have to relaunch the app after enabling TV out in the hardware menu in the simulator. On relaunch I see the second screen UILabel on the second screen. On the iPhone 4S I can only make this work if I enable AirPlay using the AirPlay control in the running tasks bar all the way to the left. You'll note that I have added an MPVolumeView component to the view on launch which I attempt to use to connect to my Apple TV to no avail.
If I use the in app MPVolumeView I can successfully disconnect from the Apple TV but when I use it again and select the apple TV to reconnect I do not get the UIScreenDidConnectNotification. Again the only time I can see the 2nd screen is when I enable mirroring. If I connect with either AirPlay control I only count 1 screen and never get the notification. Also, each time I enable mirroring I see this in the console logs:
Could not find mapped image ColorOnGrayShadow_AirPlay.png
This seems to be broken and I would like to know what others experience has been in this area. I'll continue to toy around and update this post as I learn new things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MPVolumeView 的路由菜单只能控制前往 AppleTV 的音频流。
这是您正在尝试执行的操作的示例:
https://github.com/quellish/AirplayDemo/
“ColorOnGrayShadow_AirPlay.png”日志消息正常适用于 iOS 5。不幸的是,使用 Airplay 连接的 AppleTV 作为外部显示器需要镜像,并且您无法从应用程序内部激活它。用户必须遵循以下说明:
http://support.apple.com/kb/TS4085
MPVolumeView's route menu can only control an audio stream going to the AppleTV.
Here's a working example of what you are trying to do:
https://github.com/quellish/AirplayDemo/
The "ColorOnGrayShadow_AirPlay.png" log message is normal for iOS 5. Using an Airplay connected AppleTV as an external display unfortunately requires mirroring, and you can't activate that from inside your app. The user has to follow these instructions:
http://support.apple.com/kb/TS4085