Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Exoplayer: Is it possible to use exoplayer to stream video from firebase storage?

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?

like image 689
data Avatar asked Oct 26 '25 00:10

data


1 Answers

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




            }
        });
like image 134
Priyesh Jakhmola Avatar answered Oct 27 '25 14:10

Priyesh Jakhmola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!