java - 在 Android Studio 中使用 VideoView '无法播放此视频'
问题描述
运行我的应用程序时出现“无法播放此视频”错误。寻找一些类似的问题,但问题没有解决。
(我在实际设备上运行该应用程序。我已将 minSdkVersion 设置为 23,将 targetSdkVersion 设置为 30。)
如果我需要附加任何其他代码,请告诉我。
视频活动.java:
public class VideosActivity extends AppCompatActivity {
ActivityVideosBinding binding;
ArrayList<Videos> videos;
VideosAdapter videosAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityVideosBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
videos = new ArrayList<>();
videosAdapter = new VideosAdapter(videos);
Videos videosmodel1 = new Videos("https://www.pexels.com/video/ocean-view-on-the-ship-6815712/", "Love Nature", "Nature is Love");
videos.add(videosmodel1);
Videos videosmodel2 = new Videos("https://www.pexels.com/video/aerial-shot-of-a-landscape-8820216/", "Love Nature", "Nature is Love");
videos.add(videosmodel2);
Videos videosmodel3 = new Videos("https://www.pexels.com/video/ocean-view-on-the-ship-6815712/", "Love Nature", "Nature is Love");
videos.add(videosmodel3);
Videos videosmodel4 = new Videos("https://www.pexels.com/video/ocean-view-on-the-ship-6815712/", "Love Nature", "Nature is Love");
videos.add(videosmodel4);
Videos videosmodel5 = new Videos("https://www.pexels.com/video/ocean-view-on-the-ship-6815712/", "Love Nature", "Nature is Love");
videos.add(videosmodel5);
binding.viewPagerVideos.setAdapter(videosAdapter);
}
视频型号:
public class Videos {
String videoURL, videoTitle, videoDesc;
public Videos(String videoURL, String videoTitle, String videoDesc) {
this.videoURL = videoURL;
this.videoTitle = videoTitle;
this.videoDesc = videoDesc;
}
public Videos() {
}
public String getVideoURL() {
return videoURL;
}
public void setVideoURL(String videoURL) {
this.videoURL = videoURL;
}
public String getVideoTitle() {
return videoTitle;
}
public void setVideoTitle(String videoTitle) {
this.videoTitle = videoTitle;
}
public String getVideoDesc() {
return videoDesc;
}
public void setVideoDesc(String videoDesc) {
this.videoDesc = videoDesc;
}
}
视频适配器:
public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.ViewHolder> {
ArrayList<Videos> videos;
public VideosAdapter(ArrayList<Videos> videos) {
this.videos = videos;
}
@NonNull
@NotNull
@Override
public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_video_screen, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
holder.setVideoData(videos.get(position));
}
@Override
public int getItemCount() {
return videos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//SingleVideoScreenBinding binding;
VideoView videoView;
TextView textVideoTitle, textVideoDesc;
ProgressBar videoProgressBar;
public ViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
//binding = SingleVideoScreenBinding.bind(itemView);
videoView = itemView.findViewById(R.id.videoView);
textVideoTitle = itemView.findViewById(R.id.textVideoTitle);
textVideoDesc = itemView.findViewById(R.id.textVideoDesc);
videoProgressBar = itemView.findViewById(R.id.videoProgressBar);
}
void setVideoData(Videos videosmodel){
videoView.setVideoPath(videosmodel.getVideoURL());
textVideoTitle.setText(videosmodel.getVideoTitle());
textVideoDesc.setText(videosmodel.getVideoDesc());
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoProgressBar.setVisibility(View.GONE);
mp.start();
/*float videoRatio = mp.getVideoWidth() / (float)mp.getVideoHeight();
float screenRatio = videoView.getWidth() / (float)videoView.getHeight();
float scale = videoRatio / screenRatio;
if (scale >= 1f){
videoView.setScaleX(scale);
}else {
videoView.setScaleY(1f / scale);
}*/
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
}
}
解决方案
您不能直接使用此链接,因为它不是指向实际视频本身的链接,而只是 Pexels 在其 javascript 函数中传递的 API 请求 URL,然后 Pexel 的服务器返回视频的实际链接,播放器呈现视频从那个网址。而且我们不能直接访问这个链接。
推荐阅读
- r - 如何使用 Zelig 包显示重要的星星
- python - 如何将 pd.read_sql 与参数一起使用
- zingchart - ZingChart Y 轴标签格式
- web - 我有一个 yaml 文件。我需要每天用网站数据触发,我该怎么办?
- angular - 有没有办法使用角度材料显示具有多月视图的垫子日历?
- scala - 如何在 playframework2.5 的特征中正确注入 DBApi?
- ios - 使用 Decodable 链接多个 JSON 请求 - Swift 5
- reactive-programming - 如果上游没有可用的元素,则重复 Flux 中的最后一个元素
- javascript - 如何在 React JSX 中调用 Web API?
- java - 如何通过 Onclicklistener 移动到 json 数组中的下一个 json 项