存储在 Firebase 存储中的 m3u8 文件无法在网络播放器上播放
m3u8 file which stored in Firebase storage does not play on web players
我已将 m3u8 文件存储在 firebase 云存储中,这是 URL 用于 m3u8 文件
[https://firebasestorage.googleapis.com/v0/b/testing-musicoi.appspot.com/o/MediaContent%2F240p%2F240_out.m3u8?alt=media&token=0f81947b-1df0-4333-ae67-bdf21b4c49cc]
当我在我的计算机上使用 VLC 播放器时,这个 URL 工作得很好,但是当我尝试使用同样的 link 网络播放器时,像这样 HLS media player 它不起作用。此外,我想知道这个 m3u8 URL 不能与 Android Exoplayer 一起使用吗?
网络播放器依赖于 Cross-Origin Resource Sharing,它允许一个域中的脚本访问另一个域中的数据。这会失败,因为您的源域不允许从其他域访问。
您需要配置 Access-Control-Allow-Origin
header 以允许 bitmovin.com
(和其他域)访问您的文件。
编辑您的 firebase.json
并添加或更改 hosting
部分:
"hosting": {
"headers": [ {
"source": "**/*.*",
"headers": [ {
"key": "Access-Control-Allow-Origin",
"value": "*"
} ]
} ]
}
我找到了一个类似的 question,您可能会感兴趣:
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);
}
});
我已将 m3u8 文件存储在 firebase 云存储中,这是 URL 用于 m3u8 文件
[https://firebasestorage.googleapis.com/v0/b/testing-musicoi.appspot.com/o/MediaContent%2F240p%2F240_out.m3u8?alt=media&token=0f81947b-1df0-4333-ae67-bdf21b4c49cc]
当我在我的计算机上使用 VLC 播放器时,这个 URL 工作得很好,但是当我尝试使用同样的 link 网络播放器时,像这样 HLS media player 它不起作用。此外,我想知道这个 m3u8 URL 不能与 Android Exoplayer 一起使用吗?
网络播放器依赖于 Cross-Origin Resource Sharing,它允许一个域中的脚本访问另一个域中的数据。这会失败,因为您的源域不允许从其他域访问。
您需要配置 Access-Control-Allow-Origin
header 以允许 bitmovin.com
(和其他域)访问您的文件。
编辑您的 firebase.json
并添加或更改 hosting
部分:
"hosting": {
"headers": [ {
"source": "**/*.*",
"headers": [ {
"key": "Access-Control-Allow-Origin",
"value": "*"
} ]
} ]
}
我找到了一个类似的 question,您可能会感兴趣:
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);
}
});