ExoPlayer,MediaSource创建了视频播放器,无法播放从URL提取的视频

时间:2018-06-22 04:26:01

标签: java android android-studio exoplayer media-source

我正在尝试使用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`:

enter image description here

`MainActivity.java`:

enter image description here

1 个答案:

答案 0 :(得分:2)

您只需更改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"));