首页 > 解决方案 > 在 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();
            }
        });
    }
}

}

标签: javaandroidandroid-studioandroid-layoutandroid-videoview

解决方案


您不能直接使用此链接,因为它不是指向实际视频本身的链接,而只是 Pexels 在其 javascript 函数中传递的 API 请求 URL,然后 Pexel 的服务器返回视频的实际链接,播放器呈现视频从那个网址。而且我们不能直接访问这个链接。


推荐阅读