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

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

एंड्रॉइड ऐप में YouTube वीडियो प्ले करना

यह उदाहरण दिखाता है कि RecyclerView और WebView का उपयोग करके एंड्रॉइड ऐप में YouTube वीडियो की सूची कैसे प्रदर्शित करें और चलाएं।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: निर्भरताएं (Dependencies) जोड़ें

build.gradle (Module: app) में निम्न निर्भरताएं जोड़ें:

dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.cardview:cardview:1.0.0'
    // WebView के लिए किसी अतिरिक्त लाइब्रेरी की आवश्यकता नहीं
}

चरण 3: मुख्य लेआउट बनाएं

res/layout/activity_main.xml:

<?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="16dp"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

चरण 4: वीडियो आइटम लेआउट बनाएं

res/layout/video_view.xml (WebView आधारित):

<?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" />

चरण 5: डेटा मॉडल क्लास

YouTubeVideos.java:

public class YouTubeVideos {
    private String videoUrl;

    public YouTubeVideos() {}

    public YouTubeVideos(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public String getVideoUrl() { return videoUrl; }
    public void setVideoUrl(String videoUrl) { this.videoUrl = videoUrl; }
}

चरण 6: RecyclerView एडाप्टर

VideoAdapter.java:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter {
    private List youtubeVideoList;

    public VideoAdapter(List youtubeVideoList) {
        this.youtubeVideoList = youtubeVideoList;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.video_view, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        String iframe = youtubeVideoList.get(position).getVideoUrl();
        holder.videoWeb.loadData(iframe, "text/html", "utf-8");
    }

    @Override
    public int getItemCount() {
        return youtubeVideoList.size();
    }

    static class VideoViewHolder extends RecyclerView.ViewHolder {
        WebView videoWeb;

        VideoViewHolder(View itemView) {
            super(itemView);
            videoWeb = itemView.findViewById(R.id.webView);
            videoWeb.getSettings().setJavaScriptEnabled(true);
            videoWeb.setWebChromeClient(new WebChromeClient());
        }
    }
}

चरण 7: MainActivity में डेटा सेट करें

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List youtubeVideos = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // YouTube एम्बेड URL जोड़ें
        youtubeVideos.add(new YouTubeVideos(
            ""));
        youtubeVideos.add(new YouTubeVideos(
            ""));
        // और वीडियो जोड़ें...

        VideoAdapter adapter = new VideoAdapter(youtubeVideos);
        recyclerView.setAdapter(adapter);
    }
}

चरण 8: Manifest में अनुमतियां

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_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>

नोट

यह तरीका WebView में iframe का उपयोग करता है। बेहतर प्रदर्शन और YouTube API अनुपालन के लिए YouTube Android Player API का उपयोग करने पर विचार करें।

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

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

  1. एंड्रॉइड और iOS पर वीडियो लूप प्लेबैक: चरण-दर-चरण गाइड एंड्रॉइड और iOS पर वीडियो लूप प्लेबैक: चरण-दर-चरण गाइड

    क्या आप यह पता लगाने की कोशिश कर रहे हैं कि एंड्रॉइड पर वीडियो को लूप में कैसे चलाएं या iOS पर? हम समझते हैं कि जब आप किसी विशेष वीडियो को लूप पर चलाना चाहते हैं तो यह भ्रमित करने वाला हो सकता है क्योंकि सभी वीडियो प्लेयर्स में यह लूप फीचर नहीं होता। लेकिन चिंता न करें, इस छोटे गाइड के साथ हम आपकी म

  1. Android और iOS पर YouTube ऑडियो को बैकग्राउंड में चलाएं - आसान चरण Android और iOS पर YouTube ऑडियो को बैकग्राउंड में चलाएं - आसान चरण

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