gpt4 book ai didi

java - ExoPlayer,MediaSource 创建的视频播放器不播放从 URL 中提取的视频

转载 作者:行者123 更新时间:2023-11-29 18:50:59 25 4
gpt4 key购买 nike

我正在尝试使用 MediaSource 创建一个 ExoPlayer 视频播放器。我创建了一个简单的视频播放器来播放从在线服务器中提取的 .mp4 视频文件。我创建的应用程序正常打开,我没有收到任何错误,但我的视频播放器没有播放视频。请帮助我。

我的 MainActivity.java 代码:

package com.example.amans.my_video_player;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;

public class MainActivity extends AppCompatActivity
{
SimpleExoPlayerView simpleExoPlayerView;
SimpleExoPlayer player;
DefaultTrackSelector trackSelector;
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
boolean shouldAutoPlay = true;
DataSource.Factory mediaDataSourceFactory;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
simpleExoPlayerView.requestFocus();

TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

simpleExoPlayerView.setPlayer(player);

player.setPlayWhenReady(shouldAutoPlay);

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Application Name"), bandwidthMeter);

MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));

player.prepare(mediaSource);
}
}

我的activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/player_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我的 build.gradle 添加了依赖:

dependencies
{
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.android.exoplayer:exoplayer:2.8.1'
implementation 'com.google.android.exoplayer:exoplayer-core:2.8.1'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.8.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.1'
}

我遇到的问题:

`Dependency`:

enter image description here

`MainActivity.java`:

enter image description here

最佳答案

你只需要改变 mediasource 行,它已经过测试。

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "Application Name"), defaultBandwidthMeter);

MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));

关于java - ExoPlayer,MediaSource 创建的视频播放器不播放从 URL 中提取的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com