@@ -14,6 +14,7 @@ protocol VideoViewDelegate: class {
1414
1515class VideoView : UIView {
1616 static let playerItemStatusKeyPath = " status "
17+ static let audioSessionVolumeKeyPath = " outputVolume "
1718 weak var delegate : VideoViewDelegate ?
1819 var playerCurrentItemStatus = AVPlayerItemStatus . unknown
1920
@@ -42,6 +43,7 @@ class VideoView: UIView {
4243
4344 fileprivate var shouldRegisterForStatusNotifications = true
4445 fileprivate var shouldRegisterForFailureOrEndingNotifications = true
46+ fileprivate var shouldRegisterForOutputVolume = true
4547
4648 fileprivate var slowMotionTimeObserver : Any ?
4749 fileprivate var playbackProgressTimeObserver : Any ?
@@ -81,6 +83,17 @@ class VideoView: UIView {
8183 }
8284
8385 override func observeValue( forKeyPath keyPath: String ? , of object: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context: UnsafeMutableRawPointer ? ) {
86+
87+ if keyPath == VideoView . audioSessionVolumeKeyPath {
88+ do {
89+ try AVAudioSession . sharedInstance ( ) . setCategory ( AVAudioSessionCategoryPlayback, with: [ ] )
90+ }
91+ catch let error {
92+ print ( " Failed to start playback sound: \( error. localizedDescription) " )
93+ }
94+ return
95+ }
96+
8497 guard let playerItem = object as? AVPlayerItem else { return }
8598 self . playerCurrentItemStatus = playerItem. status
8699
@@ -138,6 +151,15 @@ class VideoView: UIView {
138151
139152 self . shouldRegisterForStatusNotifications = false
140153 currentItem. addObserver ( self , forKeyPath: VideoView . playerItemStatusKeyPath, options: [ ] , context: nil )
154+
155+ do {
156+ let audioSession = AVAudioSession . sharedInstance ( )
157+ try audioSession. setActive ( true )
158+ audioSession. addObserver ( self , forKeyPath: VideoView . audioSessionVolumeKeyPath, options: . new, context: nil )
159+ self . shouldRegisterForOutputVolume = false
160+ } catch {
161+ print ( " Failed to activate audio session " )
162+ }
141163 }
142164
143165 if self . shouldRegisterForFailureOrEndingNotifications {
@@ -163,6 +185,8 @@ class VideoView: UIView {
163185 self . playerLayer. player? . pause ( )
164186 self . playerLayer. player? . seek ( to: kCMTimeZero)
165187 self . playerLayer. player = nil
188+
189+ try ? AVAudioSession . sharedInstance ( ) . setCategory ( AVAudioSessionCategorySoloAmbient, with: [ ] )
166190 }
167191
168192 func play( ) {
@@ -335,6 +359,11 @@ extension VideoView {
335359 NotificationCenter . default. removeObserver ( self , name: . AVPlayerItemPlaybackStalled, object: nil )
336360 NotificationCenter . default. removeObserver ( self , name: . AVPlayerItemDidPlayToEndTime, object: nil )
337361 }
362+
363+ if self . shouldRegisterForOutputVolume == false {
364+ self . shouldRegisterForOutputVolume = true
365+ AVAudioSession . sharedInstance ( ) . removeObserver ( self , forKeyPath: VideoView . audioSessionVolumeKeyPath)
366+ }
338367 }
339368
340369 // When the video is having troubles buffering it might trigger the "AVPlayerItemPlaybackStalled" notification
0 commit comments