4

In android there is a library called Exoplayer that has to do with streaming a video from a given url.

Now according to this firebase doesn't support video streaming, eventhough you can pass a uri from the url to the videoview (and it will actually stream).

Question:

Can exoplayer be used to stream a video from firebase storage?

Why firebase states streaming is not possible eventhough it can be done with a videoview?

data
  • 739
  • 6
  • 17
  • if `VideoView` can do that then `ExpoPlayer` will support that as well - all you need is to use a valid [DataSource](https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/DataSource.html) – pskink Jul 22 '18 at 08:06
  • @pskink I thought like you, but then reading answers from firebase team they state that it is not possible to stream? Its confusing? – data Jul 22 '18 at 08:08
  • ask google for `firebase streaming` then – pskink Jul 22 '18 at 08:17
  • @pskink yes I will ask. – data Jul 22 '18 at 08:20
  • Did you find any solution for video streaming? As per the google's documentations firebase storage does not supports video streaming but google cloud storage supports it. Unable to find any example or tutorial to stream stored video from google cloud storage. Please let me know if anyone having idea about it. – Tejas Shelke Sep 06 '18 at 07:27

1 Answers1

2

yes its possible to stream videos from firebase.

first create an exoplayer in your xml file

<com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="0sp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

i have set the height as 0 cuz im gonna fix the height programmatically later

then declare simple SimpleExoPlayer in your activity as player.

 SimpleExoPlayer player;

then in you follow these steps

//declare your PlayerView 

final PlayerView playerView = mview.findViewById(R.id.video_view);

//your database ref

        final StorageReference storageReference =
                FirebaseStorage.getInstance().getReference("/Post_Video/"+ video + ".mp4");


 player = ExoPlayerFactory.newSimpleInstance(MainActivity.this);
        playerView.setPlayer(player);

        playerView.setVisibility(View.VISIBLE);

        playerView.getLayoutParams().height=550;
        playerView.getLayoutParams().width=950;



        storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {


                BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(MainActivity.this).build();
                TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
                ExoPlayer exoPlayer = (SimpleExoPlayer) ExoPlayerFactory.newSimpleInstance(MainActivity.this);
                Uri video = Uri.parse(uri.toString());
                DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("video");
                ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
                MediaSource mediaSource = new ExtractorMediaSource(video,dataSourceFactory,extractorsFactory,null,null);
                playerView.setPlayer(exoPlayer);
                exoPlayer.prepare(mediaSource);
                exoPlayer.setPlayWhenReady(false);




            }
        });
desertnaut
  • 57,590
  • 26
  • 140
  • 166