2017-05-13 121 views
2

我正在使用內置視頻播放器的應用程序播放mp4來自磁盤的視頻,並且我正在嘗試使其播放繼續播放音頻(視頻)時應用進入背景iOS使AVPlayer繼續在後臺播放

我讀過很多其他堆棧溢出問題,但沒有答案爲我的案件工作。

我已經加入:

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) 
    } 
} 

此外,我也有所需的背景模式在應用程序的Info.plist文件密鑰App plays audio or streams audio/video using AirPlay。 但是,AVPlayer在應用程序進入後臺時停止。

任何人都知道應用程序可能會出現什麼問題?

謝謝:)

編輯:注:我使用AVPlayerLayer連同AVPlayer。也許這是問題?

回答

2

是的AVPlayerLayer可能會造成問題。您需要將其從AVPlayer對象中刪除。在應用程序轉到後臺之前設置爲nil

兩種方式根據蘋果文檔解決,

  1. 禁用玩家項目的視頻軌(基於文件的內容只)。

  2. 從其關聯的AVPlayer中移除AVPlayerLayer(設置 AVPlayerLayer播放器屬性爲零)。

請參閱本link獲取更多信息。

+0

謝謝,解決了這個問題。 :) –