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

查看:110
本文介绍了在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.

这是我的 Adapter 代码

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上获得此信息:

显示已记录的信息"后,将显示以下警告

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类,该类在

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.

使用自定义的 Optimized VideoView ,视频将像我们的Instagram,Facebook,Twitter一样在ListView中滚动播放.我们可以通过在活动中的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天全站免登陆