mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.2 KiB
Swift
87 lines
2.2 KiB
Swift
7 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
7 years ago
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import AVFoundation
|
||
|
|
||
|
@objc
|
||
5 years ago
|
public protocol OWSVideoPlayerDelegate: class {
|
||
7 years ago
|
func videoPlayerDidPlayToCompletion(_ videoPlayer: OWSVideoPlayer)
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public class OWSVideoPlayer: NSObject {
|
||
|
|
||
7 years ago
|
@objc
|
||
5 years ago
|
public let avPlayer: AVPlayer
|
||
7 years ago
|
let audioActivity: AudioActivity
|
||
7 years ago
|
|
||
7 years ago
|
@objc
|
||
5 years ago
|
public weak var delegate: OWSVideoPlayerDelegate?
|
||
7 years ago
|
|
||
5 years ago
|
@objc public init(url: URL) {
|
||
7 years ago
|
self.avPlayer = AVPlayer(url: url)
|
||
7 years ago
|
self.audioActivity = AudioActivity(audioDescription: "[OWSVideoPlayer] url:\(url)", behavior: .playback)
|
||
7 years ago
|
|
||
|
super.init()
|
||
|
|
||
|
NotificationCenter.default.addObserver(self,
|
||
|
selector: #selector(playerItemDidPlayToCompletion(_:)),
|
||
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
||
|
object: avPlayer.currentItem)
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK: Dependencies
|
||
|
|
||
|
var audioSession: OWSAudioSession {
|
||
|
return Environment.shared.audioSession
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK: Playback Controls
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public func pause() {
|
||
|
avPlayer.pause()
|
||
7 years ago
|
audioSession.endAudioActivity(self.audioActivity)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public func play() {
|
||
7 years ago
|
let success = audioSession.startAudioActivity(self.audioActivity)
|
||
7 years ago
|
assert(success)
|
||
7 years ago
|
|
||
|
guard let item = avPlayer.currentItem else {
|
||
7 years ago
|
owsFailDebug("video player item was unexpectedly nil")
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if item.currentTime() == item.duration {
|
||
|
// Rewind for repeated plays, but only if it previously played to end.
|
||
6 years ago
|
avPlayer.seek(to: CMTime.zero)
|
||
7 years ago
|
}
|
||
|
|
||
|
avPlayer.play()
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
7 years ago
|
public func stop() {
|
||
|
avPlayer.pause()
|
||
6 years ago
|
avPlayer.seek(to: CMTime.zero)
|
||
7 years ago
|
audioSession.endAudioActivity(self.audioActivity)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc(seekToTime:)
|
||
|
public func seek(to time: CMTime) {
|
||
|
avPlayer.seek(to: time)
|
||
|
}
|
||
|
|
||
|
// MARK: private
|
||
|
|
||
|
@objc
|
||
|
private func playerItemDidPlayToCompletion(_ notification: Notification) {
|
||
|
self.delegate?.videoPlayerDidPlayToCompletion(self)
|
||
7 years ago
|
audioSession.endAudioActivity(self.audioActivity)
|
||
7 years ago
|
}
|
||
|
}
|