This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers) //For playing volume when phone is on silent | |
} catch { | |
print(error.localizedDescription) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AVPlayerViewController { | |
open override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
self.player?.pause() | |
NotificationCenter.default.post(name: Notification.Name("avPlayerDidDismiss"), object: nil, userInfo: nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func expandVideo() { | |
player?.pause() | |
let controller = AVPlayerViewController() | |
controller.player = player | |
NotificationCenter.default.addObserver(self, selector: #selector(avPlayerClosed), name: Notification.Name("avPlayerDidDismiss"), object: nil) | |
self.parentViewController()?.present(controller, animated: true) { in | |
DispatchQueue.main.async { | |
player?.play() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let controller = AVPlayerViewController() | |
controller.player = player //AVPlayer object | |
self.present(controller, animated: true) {[weak self] in | |
DispatchQueue.main.async { | |
player?.play() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if keyPath == "timeControlStatus", let change = change, let newValue = change[NSKeyValueChangeKey.newKey] as? Int, let oldValue = change[NSKeyValueChangeKey.oldKey] as? Int { | |
let oldStatus = AVPlayer.TimeControlStatus(rawValue: oldValue) | |
let newStatus = AVPlayer.TimeControlStatus(rawValue: newValue) | |
if newStatus != oldStatus { | |
DispatchQueue.main.async {[weak self] in | |
if newStatus == .playing || newStatus == .paused { | |
self?.loaderView.isHidden = true | |
} else { | |
self?.loaderView.isHidden = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let urls = [URL]() | |
//Add items to urls array | |
var playerItems = [AVPlayerItem]() | |
urls.forEach { (url) in | |
let asset = AVAsset(url: url) | |
let playerItem = AVPlayerItem(asset: asset) | |
playerItems.append(playerItem) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: 2), queue: DispatchQueue.main) {[weak self] (progressTime) in | |
if let duration = player.currentItem?.duration { | |
let durationSeconds = CMTimeGetSeconds(duration) | |
let seconds = CMTimeGetSeconds(progressTime) | |
let progress = Float(seconds/durationSeconds) | |
DispatchQueue.main.async { | |
self?.progressBar.progress = progress | |
if progress >= 1.0 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rewindVideo(by seconds: Float64) { | |
if let currentTime = player?.currentTime() { | |
var newTime = CMTimeGetSeconds(currentTime) - seconds | |
if newTime <= 0 { | |
newTime = 0 | |
} | |
player?.seek(to: CMTime(value: CMTimeValue(newTime * 1000), timescale: 1000)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func playVideo() { | |
player?.play() | |
} | |
public func pauseVideo() { | |
player?.pause() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Create a URL | |
if let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4") { | |
//2. Create AVPlayer object | |
let asset = AVAsset(url: url) | |
let playerItem = AVPlayerItem(asset: asset) | |
let player = AVPlayer(playerItem: playerItem) | |
//3. Create AVPlayerLayer object | |
let playerLayer = AVPlayerLayer(player: player) | |
playerLayer.frame = self.videoView.bounds //bounds of the view in which AVPlayer should be displayed |
NewerOlder