You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift:如何以编程方式退出Apple PiP并强制返回应用?

如何以编程方式退出Apple画中画(PiP)并返回应用

当然有办法实现,针对iOS平台的Apple PiP功能,你可以通过AVKit框架提供的API主动退出PiP模式,具体操作如下:

  • 监听播放完成事件:通过监听AVPlayerItemDidPlayToEndTimeNotification通知,捕获视频播放完成的时机。
  • 主动退出PiP:调用AVPlayerViewController的exitPictureInPicture()方法,该方法会直接关闭画中画窗口,并将用户带回你的应用界面。

代码示例(Swift)

// 在初始化播放器等合适时机添加播放完成通知监听
NotificationCenter.default.addObserver(
    self,
    selector: #selector(handleVideoPlaybackCompletion),
    name: .AVPlayerItemDidPlayToEndTime,
    object: yourPlayer.currentItem
)

@objc private func handleVideoPlaybackCompletion() {
    // 检查当前是否处于PiP激活状态,若则执行退出操作
    if yourPlayerViewController.isPictureInPictureActive {
        yourPlayerViewController.exitPictureInPicture()
    }
}

注意事项

  • 确保你的应用已开启PiP权限:需在Info.plist中配置NSPictureInPictureUsageDescription,并通过AVPlayerViewController启用PiP功能。
  • 调用exitPictureInPicture()后,系统会自动将应用切换到前台,无需额外的强制返回操作。
  • 若使用自定义PiP实现(非AVKit官方方案),需根据对应框架的API做适配处理,但官方AVPlayerViewController的方式是最稳定可靠的。

内容的提问来源于stack exchange,提问作者Mike Simz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:04:52