我正在尝试创建一个加载youtube视频的应用,但是当我运行该应用时,该应用没有显示,我得到的只是一个空白屏幕,可以滚动。我不知道为什么它没有出现。任何帮助将不胜感激。我已经包括了2个xml文件和为此使用的java类。
Videos.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/video_view">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Videos_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/videoWebView"
android:layout_height="match_parent"
android:layout_width="match_parent">
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
YoutubeVideos.class
public class YoutubeVideos {
String videoUrl;
public YoutubeVideos() {
}
public YoutubeVideos(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
}
VideoAdapter.class
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;
import java.util.List;
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder>{
List<YoutubeVideos> youtubeVideoList;
public VideoAdapter() {
}
public VideoAdapter(List<YoutubeVideos> youtubeVideoList) {
this.youtubeVideoList = youtubeVideoList;
}
@Override
public VideoViewHolder onCreateViewHolder(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(VideoViewHolder holder, int position) {
holder.videoWeb.loadData(youtubeVideoList.get(position).getVideoUrl(), "text/html", "utf-8");
}
@Override
public int getItemCount() {
return youtubeVideoList.size();
}
public class VideoViewHolder extends RecyclerView.ViewHolder {
WebView videoWeb;
public VideoViewHolder(View itemView) {
super(itemView);
videoWeb = (WebView) (itemView.findViewById(R.id.videoWebView));
videoWeb.getSettings().setJavaScriptEnabled(true);
videoWeb.setWebChromeClient(new WebChromeClient());
}
}
}
VideoActivity.class
public class VideoActivity extends AppCompatActivity {
private RecyclerView recyclerView;
Vector<YoutubeVideos> youtubeVideos = new Vector<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos);
//this leaves the keyboard hidden on load
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
getSupportActionBar().setTitle(R.string.title_videos); // for set actionbar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=fX173Ppe4bY\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=ShmBRvnnPNs\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=-SmA6_q0gP0\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=8FfmX36BY4w\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=W7vsULnwvQA\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=w3mV1nSMh8E\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=Baz0cbF2FeU\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=Olc4U_UpnSE\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=Lq_2BZXORts\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=PW7Kyjx5w74\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=LnoizmhXCxM\" frameborder=\"0\" allowfullscreen></iframe>"));
youtubeVideos.add(new YoutubeVideos("<iframe width=\"100%\" height=\"100%\" aro=\"https://www.youtube.com/watch?v=0p_lEix7PKM\" frameborder=\"0\" allowfullscreen></iframe>"));
VideoAdapter videoAdapter = new VideoAdapter(youtubeVideos);
recyclerView.setAdapter(videoAdapter);
}
答案 0 :(得分:0)
<uses-permission android:name="android.permission.INTERNET"/>
YoutubeVideos.class
public class YoutubeVideos {
String url;
public YoutubeVideos() {
}
public YoutubeVideos(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
/**
*
* @return Youtube Frame to be attached into a Webview.
* @description This helper function will allow you to generate web url with predefined settings. You can modify the height, width and other parameters.
*/
public String getFrame(){
return "<iframe width=\"100%\" height=\"100%\" src=\""+ url +"\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
}
}
VideoAdapter.class
@Override
public void onBindViewHolder(VideoViewHolder holder, int position) {
// Load frame of video in web view.
holder.videoWeb.loadData(youtubeVideoList.get(position).getFrame(), "text/html", "utf-8");
}
最后,更新您的“您”活动以仅放置视频链接。
VideoActivity.class
// Pass only the URL of video
youtubeVideos.add(new YoutubeVideos("https://www.youtube.com/embed/Bey4XXJAqS8"));