gpt4 book ai didi

java - ExoPlayer 创建的视频播放器无法正常工作

转载 作者:行者123 更新时间:2023-12-02 11:05:22 24 4
gpt4 key购买 nike

我正在尝试使用 ExoPlayer API 创建一个视频播放器。我的应用使用 API 23:Android 6.0 (Marshmallow)实现“com.google.android.exoplayer:exoplayer:2.8.1”。我正在通过在线服务器播放 .mp4 视频文件。当我尝试在 Android Studio 模拟器上运行我的应用程序时,该应用程序无法打开。我的所有代码都在下面,我已经上传了我可以看到的两个错误的图片。请帮我。我无法解决该问题,因为为 ExoPlayer 提供的明确信息很少。

我的MainActivity.java:

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.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;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

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

@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'
}

错误(我认为它们是问题所在):

1. There is a crossed line on the SimpleExoPlayerView. I don't know what this means.

enter image description here

2. There is a problem with the dependencies. 

enter image description here

最佳答案

这是我的实现,它可以对您及其对我的项目的工作有所帮助。

 public class MainActivity extends AppCompatActivity implements View.OnClickListener, PlaybackPreparer, PlayerControlView.VisibilityListener {

private PlayerView playerView;
private ExoPlayer player;
private PlayerView playerView;
private Handler mainHandler;
private DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
private DataSource.Factory mediaDataSourceFactory;
protected String userAgent;
private TrackSelector trackSelector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


playerView = view.findViewById(R.id.player_view);

mainHandler = new Handler();
userAgent = Util.getUserAgent(getContext(), "appName");
mediaDataSourceFactory = buildDataSourceFactory();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getActivity()), trackSelector, new DefaultLoadControl());
playerView.setPlayer(player);
playerView.requestFocus();
playVideoInit();

}
private DataSource.Factory buildDataSourceFactory() {
return buildDataSourceFactory(bandwidthMeter);
}

public DataSource.Factory buildDataSourceFactory(TransferListener < ? super DataSource > listener) {
if (getContext() != null)
return new DefaultDataSourceFactory(getContext(), listener, buildHttpDataSourceFactory(listener));
return null;
}

public HttpDataSource.Factory buildHttpDataSourceFactory(
TransferListener < ? super DataSource > listener) {
return new DefaultHttpDataSourceFactory(userAgent, listener, 60 * 1000,
60 * 1000, true);
}

private MediaSourceEventListener listener = new MediaSourceEventListener() {
@Override
public void onLoadStarted(DataSpec dataSpec, int dataType, int trackType,
Format trackFormat, int trackSelectionReason,
Object trackSelectionData, long mediaStartTimeMs,
long mediaEndTimeMs, long elapsedRealtimeMs) {
Log.e("ExoPlayer", "onLoadStarted");
}

@Override
public void onLoadCompleted(DataSpec dataSpec, int dataType, int trackType,
Format trackFormat, int trackSelectionReason,
Object trackSelectionData, long mediaStartTimeMs,
long mediaEndTimeMs, long elapsedRealtimeMs,
long loadDurationMs, long bytesLoaded) {
Log.e("ExoPlayer", "onLoadCompleted Load Duration: " + loadDurationMs);
}

@Override
public void onLoadCanceled(DataSpec dataSpec, int dataType,
int trackType, Format trackFormat,
int trackSelectionReason, Object trackSelectionData,
long mediaStartTimeMs, long mediaEndTimeMs,
long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {
Log.e("ExoPlayer", "onLoadCanceled");
}

@Override
public void onLoadError(DataSpec dataSpec, int dataType, int trackType,
Format trackFormat, int trackSelectionReason,
Object trackSelectionData, long mediaStartTimeMs,
long mediaEndTimeMs, long elapsedRealtimeMs,
long loadDurationMs, long bytesLoaded, IOException error,
boolean wasCanceled) {
Log.e("ExoPlayer", "onLoadError");
}

@Override
public void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs) {
Log.e("ExoPlayer", "onUpstreamDiscarded");
}

@Override
public void onDownstreamFormatChanged(int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaTimeMs) {
Log.e("ExoPlayer", "onDownstreamFormatChanged");
}
};
public void reStartVideoView() {
if (player != null) {
player.seekTo(0);
player.setPlayWhenReady(true);
} else {
Log.e("ExoPlayer", "Restart not working....");
}
}
private MediaSource buildMediaSource(Uri uri) {
int type = Util.inferContentType(uri.getLastPathSegment());
switch (type) {
case C.TYPE_DASH:
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory())
.createMediaSource(uri, mainHandler, listener);
case C.TYPE_SS:
return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory())
.createMediaSource(uri, mainHandler, listener);
case C.TYPE_HLS:
Log.e("ExoPlayer", "Hls type streaming");
return new HlsMediaSource.Factory(mediaDataSourceFactory).
createMediaSource(uri, mainHandler, listener);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(uri, mainHandler, listener);

default:
{
throw new IllegalStateException("Unsupported type: " + type);
}
}
}


private void playVideoInit() {
try {
if (player != null) {
boolean haveResumePosition = resumeWindow != C.INDEX_UNSET;
player.prepare(buildMediaSource(Uri.parse(video.getContentUrl())), !haveResumePosition, false);
player.setPlayWhenReady(true);
player.seekTo(resumePosition);

}
} catch (Exception e) {
e.printStackTrace();
}


}

private void releasePlayer() {
if (player != null) {
player.release();
player = null;
}
}

@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
playVideoInit();
}
}

@Override
public void onResume() {
super.onResume();
if ((Util.SDK_INT <= 23 || player == null)) {
playVideoInit();

}
}

@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
releasePlayer();

}
}

@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}

@Override
public void onClick(View v) {

}

@Override
public void preparePlayback() {
playVideoInit();
}

@Override
public void onVisibilityChange(int visibility) {

}
}

关于java - ExoPlayer 创建的视频播放器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008384/

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