AVPlayer -Play sound while seeking/scrubbing - ios

I have a uislider attached to my avplayer, is there a way to play sound from the video while scrubbing?
var player: AVPlayer?
func createPlayer() {
player = AVPlayer()
// init player with playerItem, asset, eventually add to playerLayer ...
player?.volume = 1
}
#objc func sliderValueChanged(_ slider: UISlider) {
let sliderValue = slider.value
let seekTime = CMTimeMakeWithSeconds(Double(sliderValue), preferredTimescale: 1000)
player?.seek(to: seekTime, toleranceBefore: .zero, toleranceAfter: .zero)
}

Since the UISlider's values update continuously while you interact with it, the AVPlayer doesn't get a chance to play any audio since the seek function gets executed every time the slider's value is updated.
I used this SO example from the comments with some minor tweaks to get this working
At a high level:
You need to add a second AVPlayer which will be responsible for playback during scrubbing
When you start scrubbing, start playing the video in the second AVPlayer from where the first player currently was
As soon as you start playing the second player, it should be locked from being updated by the scrubber and it should play for a short period of of time 1/4 to 1/2 a second (experiment with the time duration)
After this time has elapsed, pause the second player and open it to be updated again - this is what creates that jagged noise
Here is a small example that gave me something close to what you are after:
import UIKit
import AVKit
class AVPlayerScrubVC: UIViewController {
let playerViewController = AVPlayerViewController()
var player: AVPlayer?
var player2: AVPlayer?
var isScrubbingLocked = false
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
title = "Player Scrub"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configurePlayer()
configureScrubber()
}
private func configurePlayer()
{
// Add your own video URL
let videoURL
= Bundle.main.url(forResource: "sample_1",
withExtension: "mp4")
if let videoURL = videoURL {
player = AVPlayer(url: videoURL)
player2 = AVPlayer(url: videoURL)
player2?.volume = 5
playerViewController.player = player
playerViewController.showsPlaybackControls = false
// Add the AVPlayerController as a child of the current
// view controller
self.addChild(playerViewController)
// Configure the player view
let playerView = playerViewController.view
playerView?.backgroundColor = .clear
playerView?.frame = self.view.bounds
// Add the AVPlayerViewController's view as a subview
// of the current view
self.view.addSubview(playerView!)
playerViewController.didMove(toParent: self)
// Start playing the content
playerViewController.player?.play()
}
}
// Configure the UISlider
private func configureScrubber() {
let slider = UISlider()
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumValue = 0
let videoTime
= Float(CMTimeGetSeconds((player?.currentItem?.asset.duration)!))
slider.maximumValue = videoTime
slider.addTarget(self, action: #selector(sliderValueChanged(_:)),
for: .valueChanged)
view.addSubview(slider)
view.addConstraints([
slider.leadingAnchor
.constraint(equalTo: view.leadingAnchor,
constant: 16),
slider.bottomAnchor
.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: -16),
slider.trailingAnchor
.constraint(equalTo: view.trailingAnchor,
constant: -16),
slider.heightAnchor
.constraint(equalToConstant: 70)
])
}
#objc
func sliderValueChanged(_ slider: UISlider) {
let sliderValue = slider.value
let seekTime = CMTimeMakeWithSeconds(Double(sliderValue),
preferredTimescale: 1000)
player?.seek(to: seekTime,
toleranceBefore: .zero,
toleranceAfter: .zero)
if !isScrubbingLocked {
audioScrub(to: seekTime)
}
}
func audioScrub(to currentTime: CMTime) {
DispatchQueue.main.async {
// Lock the scrubbing
self.isScrubbingLocked = true
// play the simultaneous player
self.player2?.play()
// make sure that it plays for at least 0.5 of a second
// before it stops to get that scrubbing effect
// Play around with the delay to get the results best for you
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// now that 1/2 of a second has passed
// (you can make it longer or shorter as you please)
// - pause the simultaneous player
self.player2?.pause()
// now move the simultaneous player to the same point as the
// original video player:
self.player2?.seek(to: currentTime)
// setting this variable back to true allows the process to be
// repeated as long as video is being scrubbed:
self.isScrubbingLocked = false
}
}
}
}

Related

How to deactivate AVPlayer in Swift when you navigate away from view

I have a navigation view from a list of video urls to a destination Videoview and then to a video player. The player uses just the AVPlayer (I don't need controls) to automatically play the video when I navigate to it.
However the video does not stop playing when I hit the 'back' button and navigate away from the PlayerView.
Any thoughts on how to fix this are appreciated.
I've included code from the Navigation view, the pass thru VideoView and the PlayerView as context. TIA.
Navigation view
NavigationView {
List(messages, id: \.self) { message in
NavigationLink(destination: VideoView(videoURL: URL(string:"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4")!, previewLength: 15)) {
Text(message)
}
}.navigationBarTitle("Answers")
VideoView code
struct VideoView: UIViewRepresentable {
var videoURL:URL
var previewLength:Double?
func makeUIView(context: Context) -> UIView {
print("making VideoView")
return PlayerView(frame: .zero, url: videoURL, previewLength: previewLength ?? 30)
}
func updateUIView(_ uiView: UIView, context: Context) {
}
Player Snippet
class PlayerView: UIView {
private let playerLayer = AVPlayerLayer()
private var previewTimer:Timer?
var previewLength:Double
init(frame: CGRect, url: URL, previewLength:Double) {
self.previewLength = previewLength
super.init(frame: frame)
// Create the video player using the URL passed in.
let player = AVPlayer(url: url)
player.volume = 2 // Will play audio if you don't set to zero
// This is the AVPlayer approach to playing video with no controls
player.play() // Set to play once created.
// Add the player to our Player Layer
playerLayer.player = player
playerLayer.videoGravity = .resizeAspectFill // Resizes content to fill whole video layer.
playerLayer.backgroundColor = UIColor.black.cgColor
previewTimer = Timer.scheduledTimer(withTimeInterval: previewLength, repeats: false, block: { (timer) in
player.seek(to: CMTime(seconds: 0, preferredTimescale: CMTimeScale(1)))
})
layer.addSublayer(playerLayer)
Here is possible approach - to use presentation mode on update, because in this scenario it is updated due to navigation back.
struct VideoView: UIViewRepresentable {
#Environment(\.presentationMode) var presentation // << here !!
var videoURL:URL
var previewLength:Double?
func makeUIView(context: Context) -> UIView {
print("making VideoView")
return PlayerView(frame: .zero, url: videoURL, previewLength: previewLength ?? 30)
}
func updateUIView(_ playerView: PlayerView, context: Context) {
// update might be called several times, so PlayerView should
// be safe for repeated calls
if presentation.wrappedValue.isPresented {
playerView.play()
} else {
playerView.stop()
}
}
}
and PlayerView should be updated to have access to player
class PlayerView: UIView {
private let playerLayer = AVPlayerLayer()
private var previewTimer:Timer?
var previewLength:Double
private var player: AVPlayer // << make property
init(frame: CGRect, url: URL, previewLength:Double) {
self.previewLength = previewLength
super.init(frame: frame)
// Create the video player using the URL passed in.
player = AVPlayer(url: url)
player.volume = 2 // Will play audio if you don't set to zero
// don't run .play() here !!!
// ... other code
}
func play() {
player.rate = 1.0
}
func stop() {
player.rate = 0.0
}
}

problems while playing video from s3 using AVplayer

Hi all i am trying to play a video from s3 using Avplayer. Now if i play the video, the video starts playback after the whole video is buffered. so I added player.automaticallyWaitsToMinimizeStalling = false, but now the video automatically pauses
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var player: AVPlayer!
var item : AVPlayerItem!
override func viewDidLoad() {
super.viewDidLoad()
item = AVPlayerItem(url: URL(string: "https://cent-churchconnect.s3-ap-southeast-2.amazonaws.com/cent-churchconnect/testAdmin/eb8cc8b5-80e0-468a-a2c9-979cf1b5ac76_toystory.mp4")!)
player = AVPlayer(playerItem: item)
let controller = AVPlayerViewController()
present(controller, animated: true) { _ in }
controller.player = player
addChildViewController(controller)
view.addSubview(controller.view)
controller.view.frame = CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 300)
controller.player = player
controller.showsPlaybackControls = true
if #available(iOS 10.0, *) {
player.automaticallyWaitsToMinimizeStalling = false
player.play()
} else {
// Fallback on earlier versions
}
}
}
I had the same issue below steps worked for me,
Try to use method func playImmediately(atRate:) and make sure the property player.automaticallyWaitsToMinimizeStalling = false is set properly.
Use method func playImmediately(atRate:) instead of func play()
I used the cocoa pod https://github.com/piemonte/Player.
You can use the delegate methods to control the playback.

In swift, how to detect touch while playing video in AVPlayerViewController

I have programmatically added an AVPlayerViewController to a UIViewController. I am able to receive the notification when the player is finished playing (playerDidFinishPlaying). I would also like to know if a user has touched the screen while the video is playing and I have not found any related notifications.
The solution is to create a Base class of AVPlayerViewController and override touches​Began(_:​with:​) method:
Swift 2:
Custom Base Class:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesBegan")
}
}
ViewController:
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = CustomAVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
Swift 3:
Custom Base Class:
class CustomAVPlayerViewController: AVPlayerViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan")
}
}
View Controller:
let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = CustomAVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
Don't forget to import AVKit and import AVFoundation.
Each time you tap on the playerViewController, "touchesBegan" will be printed.
I had this same problem. The contentOverlayView is only available in tvos, so that was not an option.
I ended up adding a UIView over the UIImageView that I added the AVPlayer to. I set the background color to clear on the UIView, so it's not visible, but can receive gestures. This provides a target for the tap gesture recognizer.
I resolve the same. Subclassing AVPlayerViewController will work on iOS 11.4.1 but not on iOS 12 an above. So the solution for this is add subview on playerviewcontroller contentoverlayview and then on that subview you can add any gesture or button for detecting the touch. Here is the code snippet for the same::
// This notification is added for continuous playing of the video you can remove this in case you need video is played only once.
private func playVideo() {
guard let path = Bundle.main.path(forResource: "BG01", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
self.player = AVPlayer(url: URL(fileURLWithPath: path))
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: kCMTimeZero)
self?.player?.play()
}
let playerController : AVPlayerViewController? = AVPlayerViewController()
let btn : UIButton = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
btn.addTarget(self, action: #selector(touchDetect), for: .touchUpInside)
btn.backgroundColor = UIColor.clear
playerController?.contentOverlayView?.addSubview(btn)
// playerController?.homeVCProtocolDelegate = self as HomeVCProtocol
playerController?.player = player
playerController?.showsPlaybackControls = false
self.player?.play()
present(playerController!, animated: false) {
self.player?.play()
}
}
#objc func touchDetect()
{
// Here you will get the call
}

AVPlayerViewController fadeout on completion

I using AVPlayerViewController which I want to fadeout when the video ends... I can capture the end-of-video event, and I the code put together for AVPlayer works, but for the AVPlayerViewController not.
self.playerItem = AVPlayerItem(URL: videoURL)
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.streamPlayer = AVPlayerViewController()
self.streamPlayer.player = self.player
self.streamPlayer.view.frame = CGRect(x: 128, y: 222, width: 512, height: 256)
This code works too, but goes full screen- I don't want full screen...
//self.presentViewController(self.streamPlayer, animated: true) {
// self.streamPlayer.player!.play()
//}
I use this?
self.view.addSubview(self.streamPlayer.view)
self.streamPlayer.player!.play()
The event capture ..
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
}
The event Code ...
func finishedPlaying(myNotification:NSNotification) {
let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1.0
fadeOut.toValue = 0.0
fadeOut.duration = 8.0
fadeOut.delegate = self
fadeOut.setValue("video", forKey:"fadeOut")
fadeOut.removedOnCompletion = false
fadeOut.fillMode = kCAFillModeForwards
This line does nothing with AVPlayerViewController/With AVPlayer it fades out nicely ?
playerLayer.addAnimation(fadeOut, forKey: nil)
This simply removes AVPlayerViewController, plan B if I cannot make this work!
//self.streamPlayer.view.removeFromSuperview()
print("VIDEO finished")
}
AVPlayerViewController has it's own AVPlayerLayer and does not know about the AVPlayerLayer you created. Thus, the line
playerLayer.addAnimation(fadeOut, forKey: nil)
has no effect. You should set or animate the alpha value of AVPlayerViewController's view. For the fade animation you may want to look at the +[UIView animateWithDuration:(...)] methods and call
self.streamPlayer.view.removeFromSuperview()
in the completion handler.

Draw button on top of AVPlayer

I have to draw a label or button on top of video relay next previous , leave comment . List of video have it, once user select one item from the table,it need to play, Once player play finished, those buttons or label should come on top of video
Here is my code :
comPlayerControl = AVPlayerViewController()
if let player = comPlayerControl {
let videoURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/"+selectedSubmission.transcodeRefId+"/format/applehttp/protocol/http/a.m3u8"
let playerItem = AVPlayerItem(URL: NSURL(string: videoURL)! )
commmentPlayer = AVPlayer(playerItem: playerItem)
player.player = commmentPlayer
player.view.frame = videoCell.frame
player.view.sizeToFit()
player.showsPlaybackControls = true
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(CommentsTableViewController.playerDidFinishPlaying(_:)),
name: AVPlayerItemDidPlayToEndTimeNotification,
object: playerItem
)
comPlayerControl.delegate = self
videoCell.addSubview(player.view)
}
func playerDidFinishPlaying(note: NSNotification) {
print("Video Finished")
let DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
DynamicView.layer.zPosition = 1;
comPlayerControl.view.addSubview(DynamicView)
}
requirement like this
You're using an AVPlayerViewController, so there's no reason to access your application's window like in Alessandro Ornano's answer. Why reinvent the wheel? Every AVPlayerViewController has a contentOverlayView property which allows you to place views between the player and the controls.
First, create a new AVPlayerItem and listen for the AVPlayerItemDidPlayToEndTimeNotification notification on that item. Load the item into your player and begin playback.
Once the item completes, the selector your specified to listen for the AVPlayerItemDidPlayToEndTimeNotification notification will be called. In that selector, access the contentOverlayView directly and add your buttons:
In some view controller or other object:
let playerVC = AVPlayerViewController()
// ...
func setupPlayer {
let playerItem = AVPlayerItem(...)
playerVC.player?.replaceCurrentItemWithPlayerItem(playerItem)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VC.itemFinished), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
self.presentViewController(playerVC, animated: true) {
self.playerVC.player?.play()
}
}
func itemFinished() {
let btn = UIButton(type: .System)
btn.addTarget(self, action: #selector(VC.buttonTapped), forControlEvents: .TouchUpInside)
self.playerVC.contentOverlayView?.addSubview(btn)
}
func buttonTapped() {
print("button was tapped")
// replay/comment logic here
}
As stated in the comments (and a rejected edit), buttons may not work in the contentOverlayView. For an alternate solution, see Pyro's answer.
You could also subclass AVPlayerViewController and do everything inside an instance of your subclass, but Apple warns against that:
Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.
I think the best way to make the avplayer buttons is explained here: IOS 8 Video Playback using AVPlayer and AVPlayerViewController .
So , I prefeer and agree with these instructions, but if you still want to
add these buttons you can try to add them to the self.window
if let app = UIApplication.sharedApplication().delegate as? AppDelegate, let window = app.window {
let myFirstButton = UIButton()
myFirstButton.setTitle("test", forState: .Normal)
window.addSubview(myFirstButton)
...
}
I recommend looking up AVPlayerLayer as a way to show buttons and other content on top of your video.
See the Advances in AVFoundation Playback WWDC presentation.
Also, check out the AVFoundationSimplePlayer-iOS example project.
Essentially, you create a view to host your player, and you make the layer behind the view into an AVPlayerLayer.
class PlayerView: UIView {
var player: AVPlayer? {
get {
return playerLayer.player
}
set {
playerLayer.player = newValue
}
}
var playerLayer: AVPlayerLayer {
return layer as! AVPlayerLayer
}
override class var layerClass: AnyClass {
return AVPlayerLayer.self
}
}
Based on the your question and from the comment/code of Ramis i have made a sample code which you may try
As mentioned by JAL the contentOverlayView should be the best option to display the control over the video in the AVPlayerController, but as per my sample demo the contentOverlayView don't have any user interaction for the buttons or other controls, as if you check in the 3D view of the AVPlayerController it has AVTouchIgnoringView/UIView in front of the contentOverlayView which may be problem in user interaction with contentOverlayView.
So another solution is to add the overlay view in the AVPlayerViewController
func addContentOverlayView() {
OverlayView.frame = CGRectMake(0,30,AVPlayerVC.view.bounds.width, 100)
OverlayView.hidden = true
OverlayView.backgroundColor = UIColor ( red: 0.5, green: 0.5, blue: 0.5, alpha: 0.379 )
let btnNext = UIButton(frame:CGRectMake(AVPlayerVC.view.bounds.width - 60,0,60,44))
btnNext.setTitle(">>", forState:.Normal)
btnNext.addTarget(self, action:"playNext", forControlEvents:.TouchUpInside)
// btnNext.layer.borderColor = UIColor ( red: 0.0, green: 0.0, blue: 1.0, alpha: 0.670476140202703 ).CGColor
// btnNext.layer.borderWidth = 1.0
OverlayView.addSubview(btnNext)
let btnReplay = UIButton(frame:CGRectMake((AVPlayerVC.view.bounds.width/2)-40,0,80,44))
btnReplay.setTitle("Replay", forState:.Normal)
btnReplay.addTarget(self, action:"replayVideo", forControlEvents:.TouchUpInside)
OverlayView.addSubview(btnReplay)
let btnPrevious = UIButton(frame:CGRectMake(0,0,80,44))
btnPrevious.setTitle("<<", forState:.Normal)
btnPrevious.addTarget(self, action:"previousVideo", forControlEvents:.TouchUpInside)
OverlayView.addSubview(btnPrevious)
let btnComment = UIButton(frame:CGRectMake((AVPlayerVC.view.bounds.width/2)-70,40,140,44))
btnComment.setTitle("Comments", forState:.Normal)
btnComment.addTarget(self, action:"openComments", forControlEvents:.TouchUpInside)
OverlayView.addSubview(btnComment)
AVPlayerVC.view.addSubview(OverlayView);
}
func playNext() {
prevItem = AVPlayerVC.player?.currentItem
OverlayView.hidden = true
commmentQueuePlayer.advanceToNextItem()
}
func replayVideo() {
OverlayView.hidden = true
AVPlayerVC.player?.currentItem?.seekToTime(kCMTimeZero)
AVPlayerVC.player?.play()
}
func previousVideo() {
OverlayView.hidden = true
if prevItem != AVPlayerVC.player?.currentItem {
if (commmentQueuePlayer.canInsertItem(prevItem!, afterItem:AVPlayerVC.player?.currentItem)) {
//commmentQueuePlayer.insertItem(prevItem!, afterItem:AVPlayerVC.player?.currentItem)
commmentQueuePlayer.replaceCurrentItemWithPlayerItem(prevItem)
prevItem = AVPlayerVC.player?.currentItem
replayVideo()
}
} else {
replayVideo()
//Else display alert no prev video found
}
}
func stopedPlaying() {
if prevItem == nil {
prevItem = AVPlayerVC.player?.currentItem
}
OverlayView.hidden = false
}
At the initial setup we set the AVPlayerController,AVQueuePlayer etc... at that time we can add the overlay on the AVPlayerController
For the previous item there is no direct available and as per documentation the item will be remove once it's next item is played , so we have two option like replaceCurrentItemWithPlayerItem or insertItem(item: AVPlayerItem, afterItem: AVPlayerItem?)
If you need to check complete code you can check it from :
https://gist.github.com/Pyrolr/debb4fca8f608b1300e099a5b3547031
Note: This is just like prototype, it is not working perfectly in all the cases but it can help you in understanding the basic functionality you wnat and you can improve/optimise based on your requirements
Check Below code, I am able to add the button on overlay as well as button event is being called.
var playerViewController: AVPlayerViewController?
var OverlayView = UIView()
private lazy var button1: UIButton = {
let selfType = type(of: self)
let button = UIButton(frame: .init(origin: .zero, size: .init(width: selfType.musicWidth, height: selfType.musicHeight)))
button.setImage(UIImage(named: "star"), for: .normal)
button.addTarget(self, action: #selector(button1DidSelect), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: selfType.musicWidth),
button.heightAnchor.constraint(equalToConstant: selfType.musicHeight)
])
return button
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
guard let url = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4") else { return }
let player = AVPlayer(url: url)
self.playerViewController = AVPlayerViewController()
guard let playerViewController = self.playerViewController else { return }
playerViewController.player = player
present(playerViewController, animated: true) {
playerViewController.player?.play()
}
self.addButtonsOnOverlayView()
self.playerViewController?.showsPlaybackControls = false
self.isHideOverlayControls = false
}
#objc private func button1DidSelect() {
print("button1 Selected")
}
private func addButtonsOnOverlayView() {
guard let overlayView = self.playerViewController?.contentOverlayView else { return }
if !self. button1.isDescendant(of: overlayView) {
overlayView.addSubview(self.musicFirstButton)
self.playerViewController?.showsPlaybackControls = true
}
NSLayoutConstraint.activate([
button1.leadingAnchor.constraint(equalTo: overlayView.leadingAnchor, constant: 20),
button1.topAnchor.constraint(equalTo: overlayView.topAnchor, constant: 20),
])
}

Resources