Display a video in a VideoView from Firebase URL in Android
Asked Answered
A

1

3

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

  • 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.

This is the excerpt from my Adapter Code:

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;
    }
}

I'm getting this on 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)

This is my VideoView Layout:

<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" />
Alberic answered 9/6, 2017 at 1:13 Comment(5)
Where is a Firebase? Have you started this app in other devices or emulators?Featherbrain
@Featherbrain That's in my MainActivity. Updated my question with that just now. Please see above.Alberic
Now that I've downloaded locally from Firebase Storage to my Mobile Storage. Got a hint from here. Now the question is, where do i set the path of the Video in MessageAdapter ? or MainActivity ? Can you please guide me. @FeatherbrainAlberic
Is it working good if you try from a local resource like a .mp4 movie stored in raw folder?Paluas
@PC. could you avoid writing comments to answers to unrelated questions with the sole purpose of linking to this question?Rifling
A
2

The culprit here is android.widget.VideoView which extends SurfaceView.

This blog says:

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.

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.

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.

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" />
Alberic answered 20/6, 2017 at 15:41 Comment(2)
I would be curious to know if it worked with RecyclerView (without the aid of an external library), but I'm glad you got ListView working.Weissman
Don't forget to link to your new optimized repository when it is ready.Weissman

© 2022 - 2024 — McMap. All rights reserved.