在android中,有一个名为 Exoplayer 的库,该库与流处理来自给定URL的视频有关。
根据this,firebase现在不支持视频流,即使您可以将uri从url传递到视频视图(它实际上也会流)。
问题:
可以使用exoplayer从Firebase存储流式传输视频吗?
为什么即使使用videoview,firebase也无法进行流传输?
答案 0 :(得分:1)
是的,可以从Firebase流式传输视频。
首先在您的xml文件中创建一个exoplayer
<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" />
我将高度设置为0,因为稍后我会通过编程方式将高度设置为
然后在您的活动中将简单的SimpleExoPlayer声明为播放器。
SimpleExoPlayer player;
然后按照以下步骤操作
//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);
}
});