I am putting n avplayer inside of a view controller to customize some other elements of the view controller but I still want the playback and scrubbing controls used in AVPlayerViewController. Is there a way to enable these controls for the Avplayer when I am not using AvPlayerViewcontroller?
AVPlayer with playback controls of avplayerviewcontroller
Hey, did you get the solution. I am adding AVPlayerViewController as child view controller and kept its frame custom. But the controls like play, pause, scrubbing controls etc are not visible. Please help. –
Dissolvent
No. The usual solution (explicitly advised by Apple) is to use the AVPlayerViewController as an embedded view controller - i.e., make your view controller a custom parent view controller and the AVPlayerViewController its child, and now you can place its view (the movie and the controls) inside your view as a subview in good order. Example (self
is your view controller):
let url = // whatever
let player = AVPlayer(URL:url)
let avPlayerViewController = AVPlayerViewController()
avPlayerViewController.player = player
avPlayerViewController.view.frame = // whatever
self.addChild(avPlayerViewController)
self.view.addSubview(avPlayerViewController.view)
avPlayerViewController.didMove(toParent: self)
While this works great, Xcode will show errors like below:Unable to simultaneously satisfy constraints. It seems like the issue is with the autolayout of playback controls within AVPlayerViewController. Do think this is a bug though. –
Katzenjammer
cant create the avplayerviewcontroller object in swift 3.0 –
Tomahawk
@Natto The Ask Question button is big, blue, and at the top right. –
Diploblastic
To update this post with Swift 5 you can try this code:
let videoURL = URL(string: "https://your_video_in_internet.mp4")
let player = AVPlayer(url: videoUrl)
let playerViewController: AVPlayerViewController!
playerViewController = AVPlayerViewController()
playerViewController.view.frame = YOUR_FRAME_OR_EMBED_IN_STORYBOARD
playerViewController.player = player
playerViewController.showsPlaybackControls = true
playerViewController.videoGravity = .resizeAspectFill
YOUR_FRAME_OR_EMBED_IN_STORYBOARD.addSubview(playerViewController.view)
playerViewController.player!.play()
This won't show playback and scrubbing controls –
Creepie
Sorry, I thought it did it, if the controls are not shown, you have to add this sentence to the AVPlayerController: "showsPlaybackControls = true" –
Coagulant
Can you edit your answer please with the controls? You did not mention anything about AVPlayerController in your solution, thus, we can't just put showPlaybackControlls = true –
Tc
Done! I have added the controller in the answer. –
Coagulant
I had to add
addChild(playerViewController)
to make that work. –
Coriecorilla let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = self.videoPlayerView.bounds
playerViewController.player = player
self.addChild(playerViewController)
self.videoPlayerView.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
© 2022 - 2024 — McMap. All rights reserved.