我正在尝试使用ExoPlayer API创建视频播放器。我的应用程序正在使用239.255.0.15
和API 23: Android 6.0 (Marshmallow)
。我正在通过在线服务器播放implementation 'com.google.android.exoplayer:exoplayer:2.8.1'
视频文件。当我尝试在Android Studio模拟器上运行我的应用程序时,该应用程序无法打开。我的所有代码都在下面,并且我上传了我看到的两个错误的图片。请帮我。我无法解决问题,因为.mp4
提供的信息很少。
我的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.
答案 0 :(得分:1)
这是我的实现,它对您及其在我的项目中的工作可能会有所帮助。
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) {
}
}