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
		
	
| 
											8 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											8 years ago
										 | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import AVFoundation | ||
|  | 
 | ||
|  | @objc | ||
| 
											5 years ago
										 | public protocol OWSVideoPlayerDelegate: class { | ||
| 
											8 years ago
										 |     func videoPlayerDidPlayToCompletion(_ videoPlayer: OWSVideoPlayer) | ||
|  | } | ||
|  | 
 | ||
|  | @objc | ||
|  | public class OWSVideoPlayer: NSObject { | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											5 years ago
										 |     public let avPlayer: AVPlayer | ||
| 
											8 years ago
										 |     let audioActivity: AudioActivity | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											5 years ago
										 |     public weak var delegate: OWSVideoPlayerDelegate? | ||
| 
											8 years ago
										 | 
 | ||
| 
											5 years ago
										 |     @objc public init(url: URL) { | ||
| 
											8 years ago
										 |         self.avPlayer = AVPlayer(url: url) | ||
| 
											7 years ago
										 |         self.audioActivity = AudioActivity(audioDescription: "[OWSVideoPlayer] url:\(url)", behavior: .playback) | ||
| 
											8 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 | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     // MARK: Playback Controls | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func pause() { | ||
|  |         avPlayer.pause() | ||
| 
											7 years ago
										 |         audioSession.endAudioActivity(self.audioActivity) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func play() { | ||
| 
											7 years ago
										 |         let success = audioSession.startAudioActivity(self.audioActivity) | ||
| 
											7 years ago
										 |         assert(success) | ||
| 
											8 years ago
										 | 
 | ||
|  |         guard let item = avPlayer.currentItem else { | ||
| 
											7 years ago
										 |             owsFailDebug("video player item was unexpectedly nil") | ||
| 
											8 years ago
										 |             return | ||
|  |         } | ||
|  | 
 | ||
|  |         if item.currentTime() == item.duration { | ||
|  |             // Rewind for repeated plays, but only if it previously played to end. | ||
| 
											7 years ago
										 |             avPlayer.seek(to: CMTime.zero) | ||
| 
											8 years ago
										 |         } | ||
|  | 
 | ||
|  |         avPlayer.play() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     public func stop() { | ||
|  |         avPlayer.pause() | ||
| 
											7 years ago
										 |         avPlayer.seek(to: CMTime.zero) | ||
| 
											7 years ago
										 |         audioSession.endAudioActivity(self.audioActivity) | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 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) | ||
| 
											8 years ago
										 |     } | ||
|  | } |