PC. PC. - 1 month ago 5
Android Question

Display a video in a VideoView from Firebase URL in Android

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" />

PC. PC.
Answer Source

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

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" />