Upload and play video in swift.
Just follow the five easy steps upload and play video.
In this story i will cover everything regarding upload and play video in swift.
Step 1: Change setting in info.plist
Step 2: Function to upload video from gallery and camera
Step 3: Button with action upload video from gallery and camera
Step 4: Upload video to server
Step 5: Now Play Video
Extra Bonus: Function to get video thumbnail
> > Change setting in info.plist
>> use this function to upload video from gallery and camera
**create a swift file and copy paste the below code**
>> Add a button with action
class UploadVideoViewController: UIViewController, UIImagePickerControllerDelegate, VideoPickerDelegate{
@IBOutlet weak var addVideo: UIButtonX!
var arrVideo : [URL] = []
func didSelect(url: URL?) {
if let y = url {
// x was not nil, and its value is now stored in y
arrVideo.append(url!)
}
}
@IBAction func addVideoClick(_ sender: Any) {
self.videoPicker = VideoPicker(presentationController: self, delegate: self)
self.videoPicker.present(from: sender as! UIView)
}
}
**when user will clicked addVideoClick, the func didSelect(url: URL?) will called, the url that we want , which will be uploaded on server.**
Upload video to server
**copy and paste the above code , change url and and parameter according to your need**
Now Play Video:-
import AVKit
import AVFoundationclass VideoController: UIViewController
override func viewDidLoad(){
guard let videoURL = URL(string: “VideoURL”) else {
return
}
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player?.play()
}}
Function to get video thumbnail:-
If you follow the all steps carefully. It will work well.
Thank you Hope this will help you.