从 Android 中的 Firebase URL 在 VideoView 中显示视频 [英] Display a video in a VideoView from Firebase URL in Android

查看:27
本文介绍了从 Android 中的 Firebase URL 在 VideoView 中显示视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run my app I'm having these issues.

  • 黑屏闪烁 2 秒,然后显示我的应用的实际意图.
  • 视频开始,当我向上/向下滚动时,VideoView 变为白色并继续播放.无法看到视频,但在后台播放.
  • 播放完成后,我收到了无法播放此视频"的对话框.
  • A black screen flashes for 2 secs and then displays the actual intent of my app.
  • The video gets started and when I scroll up/down, the VideoView turns white and continues playing. Not able to see the video but plays in background.
  • After playing is done, for few seconds I'm getting this "Can't play this video" dialog.

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

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 上得到这个:

显示Logged信息后出现如下警告

After displaying the Logged info there is a following warning as shown below

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.

这个博客说:

使用 ListView 一行内的 VideoView 播放视频起初似乎有效,直到用户尝试滚动列表.作为一旦列表开始滚动,视频就会变黑(有时显示为白色).它使在后台播放,但你看不到它了,因为它将视频的其余部分呈现为黑匣子.

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 类,它在 github 存储库.虽然它有一些问题,因为它是 3 年前写的,但我设法自己解决了它们.很快,我将使用我的自定义 Optimized VideoView github 存储库更新此答案(只需稍作修改).在此之前,我提供的链接会对您有所帮助.

I wrote my own VideoView class which extends TextureView with the help of this github repository. Though It has some issues as it was written 3 years ago I managed to fix them on my own. Very soon I'll update this answer with my custom Optimized VideoView github repository (just with few modifications). Until then, the link which I've provided would help you.

使用自定义优化视频视图,视频将在 ListView 中滚动播放,就像我们的 Instagram、Facebook、Twitter 一样.我们可以通过在我们的活动中的 VideoView 上设置 setOnTouchListener() 来使视频播放/暂停,并且可以以我们自己的方式对其进行自定义.

With the custom Optimized VideoView, the videos will play on scroll in the ListView just like our Instagram, Facebook, Twitter. We can make the videos play/pause by setting setOnTouchListener() on VideoView in our activity and can customize it in our own way.

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

Now, our VideoView in the Layouts would be:

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

这篇关于从 Android 中的 Firebase URL 在 VideoView 中显示视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆