java - ExoPlayer,MediaSource 创建的视频播放器不播放从 URL 中提取的视频
问题描述
我正在尝试ExoPlayer
使用MediaSource
. 我创建了一个简单的视频播放器来播放.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`:
`MainActivity.java`:
解决方案
您只需将 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"));
推荐阅读
- python - 使用flask.app.test_client()测试Flask应用程序时如何在python Flask中指定标头?
- php - Pimcore:保存对象的新版本但不发布此版本
- asp.net-core-identity - 如何在视图中获取 Asp.net Core Identity 用户
- python - Keras 批量训练在线预测不学习
- java - 如何使用 LambdaMetaFactory 调用构造函数?
- php - php-encrypt 比较两个数据
- r - 从文本中去除数字:R
- python - 更快的代码温度传感器 - Raspberry Pi
- html - 页脚社交图标在调整大小时不会均匀分布
- sql-server - 在实体框架中恢复 SQL Server 数据库