简体   繁体   English

iOS 启用 AVPlayer 以继续在后台播放

[英]iOS Enable AVPlayer to continue playing in background

I'm working on an app with a built in video player that plays mp4 videos from disk and I'm trying to make it continue playing the audio (of the video) when app goes into background .我正在开发一个带有内置视频播放器的应用程序,可以从磁盘播放mp4视频,我试图让它在应用程序进入后台继续播放(视频的)音频。

I've read up on many other stack overflow questions but none of the answers worked for my case.我已经阅读了许多其他堆栈溢出问题,但没有一个答案适用于我的情况。

I have added:我已经添加了:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
      print("AVAudioSession Category Playback OK")

    do {
      try AVAudioSession.sharedInstance().setActive(true)
      print("AVAudioSession is Active")
    } catch let error as NSError {
      print(error.localizedDescription)
    }
  } catch let error as NSError {
      print(error.localizedDescription)
  }
}

Besides, I also have the required background mode key App plays audio or streams audio/video using AirPlay in the app's Info.plist file.此外,我还有所需的后台模式键App plays audio or streams audio/video using AirPlay应用程序的 Info.plist 文件中的App plays audio or streams audio/video using AirPlay However, the AVPlayer stops when the app goes into background.但是,当应用程序进入后台时, AVPlayer停止。

Anyone knows what might be wrong with the app?任何人都知道该应用程序可能有什么问题?

Thanks :)谢谢 :)

Edit: Note: I'm using AVPlayerLayer together with AVPlayer .编辑:注意:我将AVPlayerLayerAVPlayer一起使用。 Maybe this is the problem?也许这就是问题所在?

Yes AVPlayerLayer might be creating issue.是的AVPlayerLayer可能会产生问题。 You need to remove it from AVPlayer object.您需要将其从AVPlayer对象中删除。 Set to nil before application go to background.在应用程序进入后台之前设置为nil

Two ways to resolve according to Apple Document,根据Apple Document的两种解决方法,

  1. Disable the video tracks in the player item (file-based content only).禁用播放器项目中的视频轨道(仅限基于文件的内容)。

  2. Remove the AVPlayerLayer from its associated AVPlayer (set the AVPlayerLayer player property to nil).从关联的 AVPlayer 中移除 AVPlayerLayer(将 AVPlayerLayer 播放器属性设置为 nil)。

Refer this link for more info.有关更多信息,请参阅此链接

First you must have enabled "Background Audio" in Capability.首先,您必须在功能中启用“背景音频”。 Follow bellow link to do so.按照下面的链接这样做。

[See image to Enable Background Audio] [查看图像以启用背景音频]

When your app moves to background, assign your player to nil.当您的应用程序移至后台时,将您的播放器分配为 nil。

// If presenting video with AVPlayerViewController
    playerViewController.player = nil

// If presenting video with AVPlayerLayer
    playerLayer.player = nil

When your app moves to foreground again, do as given bellow.当您的应用程序再次移至前台时,请按以下步骤操作。

// If presenting video with AVPlayerViewController
playerViewController.player = player

// If presenting video with AVPlayerLayer
playerLayer.player = player

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM