Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit b4e3047

Browse files
askeland3lvis
authored andcommitted
Activate sound when the volume is changed (even in silent mode) (#95)
* Activate sound when the volume is changed even if the phone is on silent mode (kind of like Snapchat). This is reset when video is closed, so volume must be changed again when new video starts if the user wants sound * Remove UIRequiredDeviceCapabilities
1 parent f31ca65 commit b4e3047

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

Source/VideoView.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protocol VideoViewDelegate: class {
1414

1515
class 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

iOS/Info.plist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
<true/>
2929
<key>UILaunchStoryboardName</key>
3030
<string>LaunchScreen</string>
31-
<key>UIRequiredDeviceCapabilities</key>
32-
<array>
33-
<string>armv7</string>
34-
</array>
3531
<key>UISupportedInterfaceOrientations</key>
3632
<array>
3733
<string>UIInterfaceOrientationPortrait</string>

0 commit comments

Comments
 (0)