Play YouTube videos in swift

Updated 26 November 2021

Save

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.

Adding assets

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.

  1. pod init
  2. open podfile

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.

Adding the code to play youtube videos in swift

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.

play youtube videos in swift

Getting Youtube Id

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

Play Youtube video in swift

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.

Getting information from the video link

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.

Conclusion

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

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home