I want to show a video accordingly to the selected cell in my tableview. I want to show the video in a view in my vc.
The following code is showing the correct videos when the cells are tapped but whenever i select a new cell the audio from the old video is still there. How can i remove it ?
Setting up the view in my vc:
func setupView(for url: URL) {
let player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoView.bounds
self.videoView.layer.addSublayer(playerLayer)
player.play()
}
didSelectRow code:
let selectedExercise = exercises[indexPath.row]
setupView(for: URL(string: selectedExercise.videoUrl)!)
My guess is that i should pause/stop the previous video before showing the new one. But i don't know how to.
Thanks a lot!
It's happend because you create new instance of AVPlayer every tap.
To solve your problem, you need to move
let player = AVPlayer(url: url)
outside of setupView() and call it only once. You could move it in init() for example.
For updating asset (video url) in setupView() you could use
player.replaceCurrentItem(with: AVPlayerItem(url: url))
.
Related
I have an UIView which contains some some texts and views. in each view I need to play a video with AVPlayer , but my problem is only the first view shows the video :
Here is my code :
func playVideo(name:String , onView:UIView) {
let path = Bundle.main.path(forResource: name, ofType: "mp4")
let videoURL = URL(fileURLWithPath: path!)
let player = AVPlayer(url: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = onView.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
onView.layer.addSublayer(playerLayer)
player.play()
}
Using the function :
override func viewDidLoad() {
super.viewDidLoad()
//ScrollView
scrollView.contentSize = menuView.frame.size
scrollView.addSubview(menuView)
//Play videos
playVideo(name: "pizza", onView: videoView1)
playVideo(name: "salad", onView: videoView2)
playVideo(name: "fries", onView: videoView3)
}
when I run the app video only play in videoView1 , any suggestion why this happens ?
I also put my code in viewWillAppear , viewDidLayoutSubviews
According to Apple doc:
class AVPlayer
Note
AVPlayer is intended for playing a single media asset at a time.
I just found an alternative solution , I replaced UIView with UIContainerView and then link them with Embed to other view controller and play video in each view controller. Now it works perfectly as I expected.
Right now my task is to stream a video from a site like this http://watchers.to/sazktfmnvzqc.html on an iPhone. (Be warned, there are tons of pop ups)
I've tried using a UIWebView to simply open up a portal for clicking the video at that URL, but honestly they're so many pop ups its actually impossible.
Is there any clever way to circumvent this that you all can think of? Like code that I could use to instantly click underneath the popup overlay or even click on the overlay the required amount of times, instantly reloading back from the new page to stream the video? Just looking for a direction to go.
Thanks!
First you have to know the exact URL of the video. Then you can use an AVPlayerViewController, which needs an AVPlayer initialised with this URL.
Example:
import AVFoundation // this you need for the `AVPlayer`
import AVKit // this is for the `AVPlayerViewController`
let viewController = AVPlayerViewController()
let videoURL = "http://you.videourl.com"
if let player = AVPlayer.playerWithURL(videoURL) as? AVPlayer {
viewController = player
}
Then you can push the view controller and play the video.
I got if from here.
Worked for me.
https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/GettingStarted/GettingStarted.html
func play (){
//Your video path URL
guard let url = URL(string: "http://www.xxx.xxx/xx/xxxx/xxxxxxx.mov") else {return}
let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}
I have a video player in an IOS App and I want to update the video when I click on a button, but I do not see how to manage this.
(Note : it's not a list of video within a queue)
Here is the code for adding the AVPlayer:
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
..
..
self.ViewForVideo.addSubview(playerViewController.view)
self.addChildViewController(playerViewController)
player.play()
I have done like this : each time I want to change the video I create a new AVPlayer and affect it to the playerViewController.player like this
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
playerViewController.player = player
I am using AVPlayerViewController in order to play a .mov file (TeethingFinal.mov) in my project. The funny thing is, I have the feature working perfectly with two other buttons on the same ViewController but for some reason will not launch the AVPlayer! The tap gesture is being recognized, because I can see the button highlight when pressed but nothing happens. I would post error message but there is not one!
#IBAction func sugarBugPlay(sender: UIButton) {
let moviePlayerController = AVPlayerViewController()
let path = NSBundle.mainBundle().pathForResource("TeethingFinal", ofType:"mov")
let url = NSURL.fileURLWithPath(path!)
let playerVC = AVPlayerViewController()
var player: AVPlayer = AVPlayer()
playerVC.player = AVPlayer(URL: url)
self.presentViewController(playerVC, animated: true, completion: nil)
}
So sometimes Xcode is funny. I created a completely new ViewController the exact same way and reattached all of my outlets and it worked!
I am able to successfully create a player but was annoyed with the initial black screen. I decided to overlay a UIImageView and hide it once the player started. This worked, but I didn't want the hassle of creating and maintaining images for all my videos.
I was able to achieve the exact results I wanted by playing and immediately pausing the player after instantiating it. The only issue was that sometimes the state of the player was getting recorded incorrectly, so when I went to start the player again, the status was listed as already "playing" even though the player was paused.
I starting looking into using AVPlayerItem seekToTime but haven't found any feasible solutions. Is there a "non hacky" way of achieving this?
If you're using an AVPlayerViewController, this is a perfect use of the player controller's contentOverlayView property. This is a UIView between the player layer and the controls exposed exactly for this purpose:
First, create the screenshot:
let asset = AVAsset(URL: URL(string: "")!) // link to some video
let imageGenerator = AVAssetImageGenerator(asset: asset)
let screenshotTime = CMTime(seconds: 1, preferredTimescale: 1)
if let imageRef = try? imageGenerator.copyCGImageAtTime(screenshotTime, actualTime: nil) {
let image = UIImage(CGImage: imageRef)
// see part 2 below
}
Now, add the image as a subview of the contentOverlayView in the player controller:
// in the same try block
let imageView = UIImageView(image: image)
let playerVC = AVPlayerViewController()
let playerItem = AVPlayerItem(asset: asset)
playerVC.player = AVPlayer(playerItem: playerItem)
self.presentViewController(playerVC, animated: true) {
playerVC.contentOverlayView?.addSubview(imageView)
// adjust the frame of your imageView to fit on the player controller's contentOverlayView
}
Then, remove the imageView subview when the player starts playing the asset, or when buffering completes.
The AVPlayerLayer associated with your player has a readyForDisplay property.
You can try to make your host view an observer for this value, and do a refresh as soon as it is set to true.
It is set to true when the first frame is ready to be rendered, so before the player has enough data to play and set its status to readyToPlay.