Browse Source

Merge branch 'community' of https://git.yishihui.com/iOS/BFFramework into community

wenweiwei 3 năm trước cách đây
mục cha
commit
540c827954

+ 20 - 0
BFFramework/Classes/BFModules/BFUtility/PQSingletoVideoPlayer.swift

@@ -29,6 +29,8 @@ public class PQSingletoVideoPlayer: NSObject {
     public var isPlaying: Bool {
         return player.isPlaying()
     }
+    public var autoResumePlayWhenEnterForeground:Bool = true
+    var shouldResumePlayWhenEnterForeground:Bool = false
 
     public lazy var player: TXVodPlayer = {
         let player = TXVodPlayer()
@@ -141,6 +143,24 @@ public class PQSingletoVideoPlayer: NSObject {
 
     override private init() {
         super.init()
+
+        NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { notice in
+            if self.isPlaying {
+                self.pausePlayer()
+                self.shouldResumePlayWhenEnterForeground = true
+            }
+        }
+        NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { notice in
+            
+            if self.autoResumePlayWhenEnterForeground && self.shouldResumePlayWhenEnterForeground {
+
+                self.shouldResumePlayWhenEnterForeground = false
+
+                self.resumePlayer()
+
+            }
+        }
+
     }
 
     override public func copy() -> Any {