2018-02-03 00:35:32 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import AVFoundation
|
|
|
|
|
|
|
|
@objc
|
|
|
|
protocol OWSVideoPlayerDelegate: class {
|
|
|
|
func videoPlayerDidPlayToCompletion(_ videoPlayer: OWSVideoPlayer)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public class OWSVideoPlayer: NSObject {
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-02-03 00:35:32 +01:00
|
|
|
let avPlayer: AVPlayer
|
2018-02-16 21:32:29 +01:00
|
|
|
let audioActivity: AudioActivity
|
2018-02-03 00:35:32 +01:00
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-02-03 00:35:32 +01:00
|
|
|
weak var delegate: OWSVideoPlayerDelegate?
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc init(url: URL) {
|
2018-02-03 00:35:32 +01:00
|
|
|
self.avPlayer = AVPlayer(url: url)
|
2018-10-23 16:40:09 +02:00
|
|
|
self.audioActivity = AudioActivity(audioDescription: "[OWSVideoPlayer] url:\(url)", behavior: .playback)
|
2018-02-03 00:35:32 +01:00
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
selector: #selector(playerItemDidPlayToCompletion(_:)),
|
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
object: avPlayer.currentItem)
|
|
|
|
}
|
|
|
|
|
2018-10-23 02:55:39 +02:00
|
|
|
// MARK: Dependencies
|
|
|
|
|
|
|
|
var audioSession: OWSAudioSession {
|
|
|
|
return Environment.shared.audioSession
|
|
|
|
}
|
|
|
|
|
2018-02-03 00:35:32 +01:00
|
|
|
// MARK: Playback Controls
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-02-03 00:35:32 +01:00
|
|
|
public func pause() {
|
|
|
|
avPlayer.pause()
|
2018-10-23 02:55:39 +02:00
|
|
|
audioSession.endAudioActivity(self.audioActivity)
|
2018-02-03 00:35:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-02-03 00:35:32 +01:00
|
|
|
public func play() {
|
2018-10-23 02:55:39 +02:00
|
|
|
let success = audioSession.startAudioActivity(self.audioActivity)
|
2018-10-23 01:17:05 +02:00
|
|
|
assert(success)
|
2018-02-03 00:35:32 +01:00
|
|
|
|
|
|
|
guard let item = avPlayer.currentItem else {
|
2018-08-27 16:27:48 +02:00
|
|
|
owsFailDebug("video player item was unexpectedly nil")
|
2018-02-03 00:35:32 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.currentTime() == item.duration {
|
|
|
|
// Rewind for repeated plays, but only if it previously played to end.
|
|
|
|
avPlayer.seek(to: kCMTimeZero)
|
|
|
|
}
|
|
|
|
|
|
|
|
avPlayer.play()
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:17:15 +02:00
|
|
|
@objc
|
2018-03-12 23:04:16 +01:00
|
|
|
public func stop() {
|
|
|
|
avPlayer.pause()
|
|
|
|
avPlayer.seek(to: kCMTimeZero)
|
2018-10-23 02:55:39 +02:00
|
|
|
audioSession.endAudioActivity(self.audioActivity)
|
2018-03-12 23:04:16 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 00:35:32 +01:00
|
|
|
@objc(seekToTime:)
|
|
|
|
public func seek(to time: CMTime) {
|
|
|
|
avPlayer.seek(to: time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: private
|
|
|
|
|
|
|
|
@objc
|
|
|
|
private func playerItemDidPlayToCompletion(_ notification: Notification) {
|
|
|
|
self.delegate?.videoPlayerDidPlayToCompletion(self)
|
2018-10-23 02:55:39 +02:00
|
|
|
audioSession.endAudioActivity(self.audioActivity)
|
2018-02-03 00:35:32 +01:00
|
|
|
}
|
|
|
|
}
|