Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 22.214.171.124 release ready!
Owner and Founder. Opencart
Hello, everyone here is a new and exciting article on how you can play youtube videos in swift.
We all need to implement the video player in our project at some point in app development.
Sometimes, we need to integrate the functionality to play the youtube links in a video player.
Here is the article that will allow you to embed the video player that plays the youtube videos.
So let’s begin with the article.
Firstly, we need to add the required asset which is adding a third-party library via Cocoapods.
Navigate to the project directory in the terminal and run the below commands.
Podfile will open in the Text editor then add pod ‘YoutubePlayer-in-WKWebView’, “~> 0.2.0”
Do not forget to save the file.
Now run the pod install command on the terminal, this will install the third-party library which will be used to play youtube video links in the project.
Firstly, import the library in your swift file like import YoutubePlayer_in_WKWebView
You will need a youtube video link to play in the player.
After that, in the storyboard add a UIView that will be used to show the player, please check the below image.
In addition, assign the WKYTPlayerView class to the UIView and create an outlet in your swift file.
Now we will need the Youtube ID from the youtube video link to play the video.
We have created an extension to get the youtube id from the link, check the below code.
We can get the youtube id as below
It’s time to play the video in the player view that we have created. Let’s write the code.
We provide the youtube Id extracted earlier to the player, the load function of the player loads the youtube video with the given youtube id.
playerViewDidBecomeReady function is used to play the video.
In viewDidDisappear we are stopping the player if the user dismisses the view.
Cheers!! Now the video will play in the player.
Our main objective which is to play the youtube link has been completed.
In this section, we will know how to get the title of the video.
Below is the function used to get the title of the video link through the video id.
Above all, we are using google API with the video id appended on the URL and hitting the API with Alamofire.
This gives us the JSON response and we extract the video title from the response and assign it to the label.
In this, article we learned to play the youtube video link in the swift project using the third-party library.
We have discussed how to get the title of the youtube video similarly, we can get the thumbnail from the video id.
That’s all for this article, we hope you enjoyed learning new things.
For more information please go through the link
For other blogs please click here
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.