AVPlayer with playback controls of avplayerviewcontroller
Asked Answered
E

3

17

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?

Ectoplasm answered 12/11, 2015 at 17:24 Comment(1)
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
D
34

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)
Diploblastic answered 12/11, 2015 at 17:32 Comment(3)
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.0Tomahawk
@Natto The Ask Question button is big, blue, and at the top right.Diploblastic
O
1

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()  
Ozalid answered 20/5, 2020 at 19:18 Comment(5)
This won't show playback and scrubbing controlsCreepie
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 = trueTc
Done! I have added the controller in the answer.Coagulant
I had to add addChild(playerViewController) to make that work.Coriecorilla
I
0
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)
Inform answered 22/9, 2020 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.