如何将故事板中的 UIButton 添加到 iOS 中 AVPlayer 的顶部?
How to add the UIButton present in storyboard to the top of AVPlayer in iOS?
我正在我的应用程序中实现类似于 Uber 的视频启动。我在故事板底部的故事板中添加了两个按钮,故事板中的间距相等。现在我正在添加 AVPlayer 来播放 NSBundle 中的视频。虽然 运行 应用程序正在播放视频,但我看不到底部的按钮。为什么会这样?请建议..
- (void)createVideoPlayer {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.volume = PLAYER_VOLUME;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = UIViewContentModeScaleToFill;
playerLayer.frame = self.playerView.layer.bounds;
[self.playerView.layer addSublayer:playerLayer];
[self.player play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}
- (void)moviePlayDidEnd:(NSNotification*)notification{
AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero];
[self.player play];
}
storyboard按钮截图:
模拟器中的输出:
您可以看到按钮丢失,注意:这些按钮的约束是正确的,我可以在每个预览中看到它们...
您可以尝试在添加 avplayer 后在按钮上调用 bringSubviewToFront。
在您的视图中添加 AVPlyer
实例后,您需要调用 bringSubviewToFront
将 Button
插座放在前面并在 AVPlayer
上可见。
[self.view bringSubviewToFront:buttonOutlet];
我正在我的应用程序中实现类似于 Uber 的视频启动。我在故事板底部的故事板中添加了两个按钮,故事板中的间距相等。现在我正在添加 AVPlayer 来播放 NSBundle 中的视频。虽然 运行 应用程序正在播放视频,但我看不到底部的按钮。为什么会这样?请建议..
- (void)createVideoPlayer {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.volume = PLAYER_VOLUME;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = UIViewContentModeScaleToFill;
playerLayer.frame = self.playerView.layer.bounds;
[self.playerView.layer addSublayer:playerLayer];
[self.player play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}
- (void)moviePlayDidEnd:(NSNotification*)notification{
AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero];
[self.player play];
}
storyboard按钮截图:
模拟器中的输出:
您可以看到按钮丢失,注意:这些按钮的约束是正确的,我可以在每个预览中看到它们...
您可以尝试在添加 avplayer 后在按钮上调用 bringSubviewToFront。
在您的视图中添加 AVPlyer
实例后,您需要调用 bringSubviewToFront
将 Button
插座放在前面并在 AVPlayer
上可见。
[self.view bringSubviewToFront:buttonOutlet];