在 Android 中显示来自 Firebase URL 的 VideoView 视频

Display a video in a VideoView from Firebase URL in Android

当我 运行 我的应用程序时,我遇到了这些问题。

这是我的适配器代码的摘录:

public class MessageAdapter extends ArrayAdapter<Message> {
    private static class ViewHolder {
        TextView authorTextView, timeStamp, messageTitle, messageTextView;
        VideoView videoView;
        ImageView photoImageView;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        // Gets the message that we are displaying at a position
        Message message = getItem(position);

        ...

        boolean isPhoto = message.getPhotoUrl() != null;
        boolean isVideo = message.getVideoUrl() != null;

        if (isPhoto) {
            // Photo Present

        } else if (isVideo) {
            // Video present
            Log.d(TAG, "Video present !");
            viewHolder.messageTextView.setVisibility(View.GONE);
            viewHolder.photoImageView.setVisibility(View.GONE);
            viewHolder.timeStamp.setVisibility(View.VISIBLE);
            viewHolder.videoView.setVisibility(View.VISIBLE);
            viewHolder.videoView.setVideoPath(message.getVideoUrl());
            viewHolder.videoView.start();
            Log.d(TAG, "Video URL : "+message.getVideoUrl());
        } else {
            // Photo and video both absent
        }
        return convertView;
    }
}

我正在 LogCat:

显示记录的信息后,会出现如下所示的警告

D: Video present !
D: Video URL : https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/photos%2Fvideo%3A34334?alt=media&token=05xf6c2f-6abc-4ab9-b696-32153fa3d0aa
W: Couldn't open https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/photos%2Fvideo%3A34334?alt=media&token=05cf6c2f-6abc-4ab9-b696-32153fa3d0aa: java.io.FileNotFoundException: No content provider: https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/photos%2Fvideo%3A34334?alt=media&token=05cx6c2f-6abc-4ab9-b696-32153fa3d0aa
I: proxyName: 0.0.0.0 0
W: finishComposingText on inactive InputConnection
D: getMetadata
I: proxyName: 0.0.0.0 0
W: info/warning (703, 0)
I: proxyName: 0.0.0.0 0
V: Inactivity, disconnecting from the service
I: proxyName: 0.0.0.0 0
I: proxyName: 0.0.0.0 0
E: error (1, -2147483648)

这是我的 VideoView 布局:

<VideoView 
    android:id = "@+id/admin_video_view"
    android:layout_width = "300dp"
    android:layout_height = "200dp"
    android:layout_weight = "1"
    android:layout_marginLeft = "10dp"
    android:paddingTop = "5dp"
    android:scaleType = "centerCrop" />

这里的 罪魁祸首 android.widget.VideoView,它扩展了 SurfaceView

This blog 说:

Playing a video using a VideoView inside of a row of a ListView seems to work at first, until the user tries to scroll the list. As soon as the list starts to scroll, the video turns black (sometimes displays white). It keeps playing in the background but you can’t see it anymore because it renders the rest of the video as a black box.

我编写了自己的 VideoView class,它在 github 存储库的帮助下扩展了 TextureView。虽然它有一些问题,因为它是 3 年前写的,但我设法自己解决了它们。很快我将使用我的自定义 Optimized VideoView github 存储库更新此答案(仅进行少量修改)。在那之前,我提供的 link 会对您有所帮助。

使用自定义 优化 VideoView,视频将在 ListView 中滚动播放,就像我们的 Instagram、Facebook、Twitter 一样。我们可以通过在 activity 中的 VideoView 上设置 setOnTouchListener() 来制作视频 play/pause,并可以按照我们自己的方式对其进行自定义。

现在,我们在布局中的 VideoView 将是:

<your.packagename.VideoView
    android:id="@+id/admin_video_view"
    android:layout_width="300dp"
    android:layout_height="300dp" />