mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
- sync speakerphone state manipulated from system call screen - Revert audio session after call failure, ensures media plays out of speaker after placing a failing call. - Replace notification with delegate pattern since we're already using delegate pattern here. - Fixes voiceover accessibility after voice memo - Avoid audio blip after pressing hangup - Rename CallAudioSession -> OWSAudioSession Going to start using it for other non-call things since we want to gather all our audio session concerns. - Resume background audio when done playing video - Extract OWSVideoPlayer which ensures audio is in proper state before playback - Move recording session logic to shared OWSAudioSession - Deactivate audio session when complete // FREEBIE
73 lines
1.8 KiB
Swift
73 lines
1.8 KiB
Swift
//
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import AVFoundation
|
|
|
|
@objc
|
|
protocol OWSVideoPlayerDelegate: class {
|
|
@available(iOSApplicationExtension 9.0, *)
|
|
func videoPlayerDidPlayToCompletion(_ videoPlayer: OWSVideoPlayer)
|
|
}
|
|
|
|
@objc
|
|
public class OWSVideoPlayer: NSObject {
|
|
|
|
let avPlayer: AVPlayer
|
|
|
|
weak var delegate: OWSVideoPlayerDelegate?
|
|
|
|
@available(iOS 9.0, *)
|
|
init(url: URL) {
|
|
self.avPlayer = AVPlayer(url: url)
|
|
|
|
super.init()
|
|
|
|
NotificationCenter.default.addObserver(self,
|
|
selector: #selector(playerItemDidPlayToCompletion(_:)),
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
object: avPlayer.currentItem)
|
|
}
|
|
|
|
// MARK: Playback Controls
|
|
|
|
@available(iOS 9.0, *)
|
|
public func pause() {
|
|
avPlayer.pause()
|
|
OWSAudioSession.shared.endAudioActivity()
|
|
}
|
|
|
|
@available(iOS 9.0, *)
|
|
public func play() {
|
|
OWSAudioSession.shared.setPlaybackCategory()
|
|
|
|
guard let item = avPlayer.currentItem else {
|
|
owsFail("\(logTag) video player item was unexpectedly nil")
|
|
return
|
|
}
|
|
|
|
if item.currentTime() == item.duration {
|
|
// Rewind for repeated plays, but only if it previously played to end.
|
|
avPlayer.seek(to: kCMTimeZero)
|
|
}
|
|
|
|
avPlayer.play()
|
|
}
|
|
|
|
@available(iOS 9.0, *)
|
|
@objc(seekToTime:)
|
|
public func seek(to time: CMTime) {
|
|
avPlayer.seek(to: time)
|
|
}
|
|
|
|
// MARK: private
|
|
|
|
@objc
|
|
@available(iOS 9.0, *)
|
|
private func playerItemDidPlayToCompletion(_ notification: Notification) {
|
|
self.delegate?.videoPlayerDidPlayToCompletion(self)
|
|
OWSAudioSession.shared.endAudioActivity()
|
|
}
|
|
}
|