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.
		
		
		
		
		
			
		
			
				
	
	
		
			69 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
| //
 | |
| //  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 {
 | |
| 
 | |
|     let avPlayer: AVPlayer
 | |
|     let audioActivity: AudioActivity
 | |
| 
 | |
|     weak var delegate: OWSVideoPlayerDelegate?
 | |
| 
 | |
|     init(url: URL) {
 | |
|         self.avPlayer = AVPlayer(url: url)
 | |
|         self.audioActivity = AudioActivity(audioDescription: "[OWSVideoPlayer] url:\(url)")
 | |
| 
 | |
|         super.init()
 | |
| 
 | |
|         NotificationCenter.default.addObserver(self,
 | |
|                                                selector: #selector(playerItemDidPlayToCompletion(_:)),
 | |
|                                                name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
 | |
|                                                object: avPlayer.currentItem)
 | |
|     }
 | |
| 
 | |
|     // MARK: Playback Controls
 | |
| 
 | |
|     public func pause() {
 | |
|         avPlayer.pause()
 | |
|         OWSAudioSession.shared.endAudioActivity(self.audioActivity)
 | |
|     }
 | |
| 
 | |
|     public func play() {
 | |
|         OWSAudioSession.shared.startPlaybackAudioActivity(self.audioActivity)
 | |
| 
 | |
|         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()
 | |
|     }
 | |
| 
 | |
|     @objc(seekToTime:)
 | |
|     public func seek(to time: CMTime) {
 | |
|         avPlayer.seek(to: time)
 | |
|     }
 | |
| 
 | |
|     // MARK: private
 | |
| 
 | |
|     @objc
 | |
|     private func playerItemDidPlayToCompletion(_ notification: Notification) {
 | |
|         self.delegate?.videoPlayerDidPlayToCompletion(self)
 | |
|         OWSAudioSession.shared.endAudioActivity(self.audioActivity)
 | |
|     }
 | |
| }
 |