Skip to content

Commit

Permalink
ExoPlayer实现无限循环播放
Browse files Browse the repository at this point in the history
  • Loading branch information
Doikki committed May 4, 2018
1 parent 489df5f commit 4851a85
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
ijkVideoView.setPlayerConfig(new PlayerConfig.Builder()
.autoRotate()//自动旋转屏幕
// .enableCache()//启用边播边存
// .enableMediaCodec()//启动硬解码
// .usingSurfaceView()//使用SurfaceView
// .enableMediaCodec()//启动硬解码
// .usingSurfaceView()//使用SurfaceView
// .setCustomMediaPlayer(new ExoMediaPlayer(this))
.build());
ijkVideoView.setUrl(intent.getStringExtra("url"));
ijkVideoView.setVideoController(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void onClick(View v) {
.autoRotate()//自动旋转屏幕
// .usingSurfaceView()//使用SurfaceView
.setCustomMediaPlayer(player)
// .setLooping()
.build());
ijkVideoView.start();

Expand Down
85 changes: 30 additions & 55 deletions app/src/main/java/com/dueeeke/dkplayer/player/ExoMediaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,19 @@
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoRendererEventListener;

import java.util.HashMap;
import java.util.Map;

import tv.danmaku.ijk.media.player.IMediaPlayer;

public class ExoMediaPlayer extends AbstractPlayer implements Player.EventListener,
VideoRendererEventListener {

private static final String TAG = "ExoMediaPlayer";
private static final int BUFFER_REPEAT_DELAY = 1_000;

private Context mAppContext;
private SimpleExoPlayer mInternalPlayer;
private DefaultRenderersFactory renderersFactory;
private MediaSource mMediaSource;
private DefaultTrackSelector mTrackSelector;
private String mDataSource;
private Surface mSurface;
private Handler mainHandler;
private Map<String, String> mHeaders = new HashMap<>();
private PlaybackParameters mSpeedPlaybackParameters;
private int lastReportedPlaybackState;
private boolean lastReportedPlayWhenReady;
Expand Down Expand Up @@ -86,7 +79,21 @@ public void start() {

@Override
public void initPlayer() {
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
DefaultTrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

// mEventLogger = new EventLogger(mTrackSelector);

// boolean preferExtensionDecoders = true;
// boolean useExtensionRenderers = true;//是否开启扩展
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode = DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER;

DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(mAppContext, null, extensionRendererMode);
DefaultLoadControl loadControl = new DefaultLoadControl();
mInternalPlayer = new SimpleExoPlayer(renderersFactory, trackSelector, loadControl);
mInternalPlayer.addListener(this);
mInternalPlayer.setVideoDebugListener(this);
}

@Override
Expand Down Expand Up @@ -140,14 +147,8 @@ private int inferContentType(String fileName) {
}

private DataSource.Factory getHttpDataSourceFactory(boolean preview) {
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(mAppContext,
TAG), preview ? null : new DefaultBandwidthMeter());
if (mHeaders != null && mHeaders.size() > 0) {
for (Map.Entry<String, String> header : mHeaders.entrySet()) {
dataSourceFactory.getDefaultRequestProperties().set(header.getKey(), header.getValue());
}
}
return dataSourceFactory;
return new DefaultHttpDataSourceFactory(Util.getUserAgent(mAppContext,
mAppContext.getApplicationInfo().name), preview ? null : new DefaultBandwidthMeter());
}

private DataSource.Factory getDataSourceFactory(boolean preview) {
Expand All @@ -166,30 +167,11 @@ public void pause() {
public void stop() {
if (mInternalPlayer == null)
return;
mInternalPlayer.release();
mInternalPlayer.stop();
}

@Override
public void prepareAsync() {
if (mInternalPlayer != null)
throw new IllegalStateException("can't prepare a prepared player");
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
mTrackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

// mEventLogger = new EventLogger(mTrackSelector);

// boolean preferExtensionDecoders = true;
// boolean useExtensionRenderers = true;//是否开启扩展
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode = DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER;

renderersFactory = new DefaultRenderersFactory(mAppContext, null, extensionRendererMode);
DefaultLoadControl loadControl = new DefaultLoadControl();
mInternalPlayer = new SimpleExoPlayer(renderersFactory, mTrackSelector, loadControl);
mInternalPlayer.addListener(this);
mInternalPlayer.setVideoDebugListener(this);
// mInternalPlayer.setAudioDebugListener(this);
// mInternalPlayer.addListener(mEventLogger);
if (mSpeedPlaybackParameters != null) {
mInternalPlayer.setPlaybackParameters(mSpeedPlaybackParameters);
}
Expand All @@ -202,17 +184,8 @@ public void prepareAsync() {

@Override
public void reset() {
if (mInternalPlayer != null) {
mInternalPlayer.release();
mInternalPlayer.removeListener(this);
mInternalPlayer = null;
}

mSurface = null;
mDataSource = null;
mIsPrepareing = true;
mIsBuffering = false;
setBufferRepeaterStarted(false);
release();
initPlayer();
}

@Override
Expand Down Expand Up @@ -241,9 +214,16 @@ public void seekTo(long time) {
@Override
public void release() {
if (mInternalPlayer != null) {
reset();
// mEventLogger = null;
mInternalPlayer.release();
mInternalPlayer.removeListener(this);
mInternalPlayer = null;
}

mSurface = null;
mDataSource = null;
mIsPrepareing = true;
mIsBuffering = false;
setBufferRepeaterStarted(false);
}

@Override
Expand All @@ -265,11 +245,6 @@ public void setSurface(Surface surface) {
mSurface = surface;
if (mInternalPlayer != null) {
mInternalPlayer.setVideoSurface(surface);
/*if (mSurface == null) {
mTrackSelector.setRendererDisabled(getVideoRendererIndex(), true);
} else {
mTrackSelector.setRendererDisabled(getVideoRendererIndex(), false);
}*/
}
}

Expand All @@ -289,8 +264,8 @@ public void setVolume(int leftVolume, int rightVolume) {

@Override
public void setLooping(boolean isLooping) {
// this.isLooping = isLooping;
// mMediaPlayer.setLooping(isLooping);
if (mInternalPlayer != null)
mInternalPlayer.setRepeatMode(isLooping ? Player.REPEAT_MODE_ALL : Player.REPEAT_MODE_OFF);
}

@Override
Expand Down

0 comments on commit 4851a85

Please sign in to comment.