एंड्रॉइड ऐप में 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 का उपयोग करने पर विचार करें।