How to play YouTube videos in my Android application using Kotlin?

Kotlin AndroidApps/ApplicationsMobile Development

This example demonstrates how to play YouTube videos in my Android application using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="2dp"
   tools:context=".MainActivity">
   <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class MainActivity : AppCompatActivity() {
   private lateinit var recyclerView:RecyclerView
   private var youtubeVideos = Vector<youTubeVideos>()
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      recyclerView = findViewById(R.id.recyclerView)
      recyclerView.setHasFixedSize(true)
      recyclerView.layoutManager = LinearLayoutManager(this)
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/X7SiuQxhAjg\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/KyJ71G2UxTQ\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/y8Rr39jKFKU\" frameborder=\"0\" allowfullscreen>lt;/iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
     ".youtube.com/embed/8Hg1tqIwIfI\" frameborder=\"0\" allowfullscreen></iframe>"))
      youtubeVideos.add(youTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www" +
      ".youtube.com/embed/uhQ7mh_o_cM\" frameborder=\"0\" allowfullscreen></iframe>"))
      val videoAdapter = VideoAdapter(youtubeVideos)
      recyclerView.adapter = videoAdapter
   }
}

Step 4 − Create a java class VideoAdapter.java and the following code

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.recyclerview.widget.RecyclerView
class VideoAdapter internal constructor(private val youtubeVideoList: List<youTubeVideos>) :
RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {
   override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
      val view = LayoutInflater.from(parent.context).inflate(R.layout.video_view, parent, false)
      return VideoViewHolder(view)
   }
   override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
      holder.videoWeb.loadData(youtubeVideoList[position].videoUrl!!, "text/html", "utf-8")
   }
   inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
      var videoWeb: WebView = itemView.findViewById(R.id.webView)
      init {
         videoWeb.settings.javaScriptEnabled = true
         videoWeb.webChromeClient = object : WebChromeClient() {
         }
      }
   }
   override fun getItemCount(): Int {
      return youtubeVideoList.size
   }
}

Step 5 − Create a java class youTubeVideos.java and the following code −

class youTubeVideos(var videoUrl: String?) {
}

Step 6 − Create a layout resource file (Video_view.xml) and add the following code

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webView"
   android:layout_width="match_parent"
   android:layout_height="180dp">
</WebView>

Step 7 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.q11">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

raja
Published on 18-Aug-2020 14:44:10
Advertisements