यह उदाहरण दर्शाता है कि Kotlin और RecyclerView का उपयोग करके Android ऐप में YouTube वीडियो कैसे एम्बेड और चलाएं।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2 - activity_main.xml में RecyclerView जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://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>
चरण 3 - 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 val youtubeVideos = mutableListOf<YouTubeVideo>()
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)
// YouTube वीडियो iframe URLs जोड़ें
youtubeVideos.add(YouTubeVideo("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/X7SiuQxhAjg\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideo("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/KyJ71G2UxTQ\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideo("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/y8Rr39jKFKU\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideo("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/8Hg1tqIwIfI\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideo("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/uhQ7mh_o_cM\" frameborder=\"0\" allowfullscreen></iframe>"))
val adapter = VideoAdapter(youtubeVideos)
recyclerView.adapter = adapter
}
}
// डेटा क्लास
data class YouTubeVideo(val videoUrl: String)
चरण 4 - VideoAdapter.kt बनाएं
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(private val videoList: List<YouTubeVideo>) :
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.webView.loadData(videoList[position].videoUrl, "text/html", "utf-8")
}
override fun getItemCount() = videoList.size
inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val webView: WebView = itemView.findViewById(R.id.webView)
init {
webView.settings.javaScriptEnabled = true
webView.webChromeClient = WebChromeClient()
}
}
}
चरण 5 - video_view.xml लेआउट बनाएं
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="180dp" />
चरण 6 - AndroidManifest.xml में इंटरनेट अनुमति जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.youtubeplayer">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application ...>
<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>
ऐप चलाएं
डिवाइस कनेक्ट करें और Run करें। RecyclerView में YouTube वीडियो की सूची दिखाई देगी जिन्हें आप स्क्रॉल करके देख सकते हैं।