You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类似Instagram的视频APP:如何实现VideoView播放Firebase指定用户视频?

Fix: Play Specific User-Uploaded Videos in Your VideoView

Hey there! Let's sort out why all your VideoViews are playing the same video—this is a common small oversight, easy to fix.

The Root Problem

Looking at your code, you're hardcoding a single Firebase video URI globally, and your setImage method doesn't actually use the video parameter you pass to it. No matter what video URL you try to load, it always uses that fixed URI, hence the same video plays everywhere.

The Solution

We need to make the method dynamic, using the passed video URL instead of the hardcoded one. Here's how to adjust your code:

Step 1: Update the Java Method

First, remove the global fixed URI (or stop referencing it) and modify the method to use the incoming video URL. I'll also rename things for clarity (since we're dealing with videos, not images):

// Remove the global str and uri variables—we don't need them anymore

public void setVideo(String videoUrl) {
    VideoView postVideo = (VideoView) mView.findViewById(R.id.post_image);
    
    // Add a safety check for invalid/empty URLs to avoid crashes
    if (videoUrl == null || videoUrl.trim().isEmpty()) {
        postVideo.setVisibility(View.GONE); // Hide the view if no valid video
        return;
    }
    
    Uri videoUri = Uri.parse(videoUrl);
    postVideo.setVideoURI(videoUri);
    postVideo.requestFocus();
    postVideo.start();
}

Step 2: Call the Method with the Correct URL

When you need to load a specific user's video (e.g., in a list item, or on a post detail screen), pass the unique Firebase storage URL for that video to setVideo(). For example:

// Say you fetch a user's video URL from Firebase Database
String userSpecificVideoUrl = "https://firebasestorage.googleapis.com/.../user123-video.mp4?alt=media&token=xyz";
setVideo(userSpecificVideoUrl);

Extra Tips for List Views (Like Instagram's Feed)

If you're using a RecyclerView or ListView to display multiple posts, make sure to:

  • In your onBindViewHolder method, pass the current post's video URL to setVideo()
  • Stop video playback when a view is recycled to save resources and prevent unexpected playback:
    @Override
    public void onViewRecycled(VideoViewHolder holder) {
        super.onViewRecycled(holder);
        holder.postVideo.stopPlayback();
    }
    

Why This Works

Now every time you call setVideo() with a different URL, the VideoView will parse that specific URI and play the corresponding user-uploaded video. No more hardcoded content!

内容的提问来源于stack exchange,提问作者Mwanaisha Salim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:50:52