我有一个内置 React Native 的 iOS 应用程序,它使用 react-native-video 包播放视频内容并允许使用 AirPlay。当应用程序进入后台(按主页按钮、锁定屏幕或打开向下滑动菜单)时,视频将暂停,您必须从控制中心或锁定屏幕媒体控件恢复播放。我知道这是 AVPlayer 的预期行为,我正在尝试从 Apple documentation 实施以下解决方案:
<块引用>删除/恢复 AVPlayerLayer 及其关联的 AVPlayer
所提供的代码片段使用 Swift,我似乎无法在我的 AppDelegate.m 文件中使用 Objective-C 找到实现此目的的完整示例。
我找到了以下代码片段,但我不确定获取播放器视图的步骤。
/* Remove the AVPlayerLayer from its associated AVPlayer
once the app is in the background. */
- (void)applicationDidEnterBackground:(UIApplication *)application {
MyPlayerLayerView *playerView = <#Get your player view#>;
[[playerView playerLayer] setPlayer:nil]; // remove the player
}
/* Restore the AVPlayer when the app is active again. */
- (void)applicationDidBecomeActive:(UIApplication *)application {
MyPlayerLayerView *playerView = <#Get your player view#>;
[[playerView playerLayer] setPlayer:_player]; // restore the player
}