首页 > 解决方案 > 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`:

在此处输入图像描述

标签: javaandroidandroid-studioexoplayermedia-source

解决方案


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

推荐阅读