Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android ऐप में YouTube वीडियो कैसे चलाएं?

यह उदाहरण दर्शाता है कि 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 वीडियो की सूची दिखाई देगी जिन्हें आप स्क्रॉल करके देख सकते हैं।

  1. एंड्रॉइड ऐप में YouTube वीडियो कैसे चलाएं? एंड्रॉइड ऐप में YouTube वीडियो कैसे चलाएं?

    एंड्रॉइड ऐप में YouTube वीडियो प्ले करना यह उदाहरण दिखाता है कि RecyclerView और WebView का उपयोग करके एंड्रॉइड ऐप में YouTube वीडियो की सूची कैसे प्रदर्शित करें और चलाएं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: निर्भरताएं (Dependencies) जो

  1. एंड्रॉइड पर YouTube वीडियो को बैकग्राउंड में कैसे चलाएं एंड्रॉइड पर YouTube वीडियो को बैकग्राउंड में कैसे चलाएं

    यदि आप YouTube के उत्साही प्रशंसक हैं और अपने एंड्रॉइड फोन पर धुनें सुनना चाहते हैं, तो आप आधिकारिक ऐप का उपयोग करके YouTube वीडियो को बैकग्राउंड में चलाने की परेशानी जानते हैं। एक वीडियो चलाते समय, ऑडियो उसी क्षण रुक जाएगा जब स्क्रीन बंद हो जाती है या फोकस बदल जाता है। इस प्रतिबंध को दरकिनार करने क

  1. Android पर YouTube वीडियो को बैकग्राउंड में कैसे चलाएं Android पर YouTube वीडियो को बैकग्राउंड में कैसे चलाएं

    YouTube, बिना किसी संदेह के, आज स्मार्टफोन के लिए सबसे अधिक उपयोग किए जाने वाले ऐप्स में से एक है। हम सभी अपने पसंदीदा चैनलों के वीडियो देखने और इस शानदार ऐप के साथ संगीत सुनने का आनंद लेते हैं। हालांकि, मैं शर्त लगा सकता हूं कि कभी-कभी आप अपने पसंदीदा YouTube कंटेंट को बैकग्राउंड में चलाना चाहते है