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

Java ExoPlayer,MediaSource创建的视频播放器不播放从URL提取的视频,java,android,android-studio,exoplayer,media-source,Java,Android,Android Studio,Exoplayer,Media Source,我正在尝试使用MediaSource创建一个ExoPlayer视频播放器。我创建了一个简单的视频播放器,用于播放从在线服务器提取的.mp4视频文件。我创建的应用程序正常打开,我没有收到任何错误,但我的视频播放器没有播放视频。请帮帮我 MyMainActivity.javacode: package com.example.amans.my_video_player; import android.net.Uri; import android.support.v7.app.AppCompatA

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

My
MainActivity.java
code:

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);
    }
}
<?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>
我的
活动\u main.xml
代码:

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);
    }
}
<?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>
我遇到的问题是:

`Dependency`:


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

我得到了
上下文
变量和
extractoremiasource.Factory
的错误。您能告诉我如何声明
上下文
变量,以及对于
工厂
它说
无法解析符号工厂
。您可以将上下文替换为getContext()或者,删除所有exoplayer实现并替换为'com.google.android.exoplayer:exoplayer:2.8.1'更新版本库。我尝试了您的方法,但这次遇到两个错误,我不知道如何修复。我已经上传了我当前的代码和上面两个错误的图片。请帮助我在较新版本的exoplayer中不推荐使用SimpleExoPlayerView,因此您必须使用PlayerView。