Skip to content

Commit

Permalink
fix exoPlayer init looper
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuoSmall committed Dec 3, 2018
1 parent 36c5405 commit d2a0d37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private String getUrl() {
//String url = "ijkhttphook:https://res.exexm.com/cw_145225549855002";

//String url = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
String url = "http://hjq-1257036536.cos.ap-shanghai.myqcloud.com/m3u8/m1/video.m3u8";
String url = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
//String url = "http://7xjmzj.com1.z0.glb.clouddn.com/20171026175005_JObCxCE2.mp4";
//String url = "http://hjq-1257036536.cos.ap-shanghai.myqcloud.com/m3u8/m1/out2.m3u8";
//String url = "http://223.110.243.138/PLTV/2510088/224/3221227177/index.m3u8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
Expand Down Expand Up @@ -165,33 +166,7 @@ public String getDataSource() {
public void prepareAsync() throws IllegalStateException {
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 = useExtensionRenderers
? (preferExtensionDecoders ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;

rendererFactory = new DefaultRenderersFactory(mAppContext, extensionRendererMode);
DefaultLoadControl loadControl = new DefaultLoadControl();
mInternalPlayer = ExoPlayerFactory.newSimpleInstance(mAppContext, rendererFactory, mTrackSelector, loadControl, null, Looper.getMainLooper());
mInternalPlayer.addListener(this);
mInternalPlayer.addAnalyticsListener(this);
mInternalPlayer.addListener(mEventLogger);
if (mSpeedPlaybackParameters != null) {
mInternalPlayer.setPlaybackParameters(mSpeedPlaybackParameters);
}
if (mSurface != null)
mInternalPlayer.setVideoSurface(mSurface);

mInternalPlayer.prepare(mMediaSource);
mInternalPlayer.setPlayWhenReady(false);
prepareAsyncInternal();
}

@Override
Expand Down Expand Up @@ -357,6 +332,39 @@ public void release() {
}
}

protected void prepareAsyncInternal() {
new Handler(Looper.getMainLooper()).post(
new Runnable() {
@Override
public void run() {
mTrackSelector = new DefaultTrackSelector();
mEventLogger = new EventLogger(mTrackSelector);
boolean preferExtensionDecoders = true;
boolean useExtensionRenderers = true;//是否开启扩展
@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode = useExtensionRenderers
? (preferExtensionDecoders ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;

rendererFactory = new DefaultRenderersFactory(mAppContext, extensionRendererMode);
DefaultLoadControl loadControl = new DefaultLoadControl();
mInternalPlayer = ExoPlayerFactory.newSimpleInstance(mAppContext, rendererFactory, mTrackSelector, loadControl, null, Looper.getMainLooper());
mInternalPlayer.addListener(IjkExo2MediaPlayer.this);
mInternalPlayer.addAnalyticsListener(IjkExo2MediaPlayer.this);
mInternalPlayer.addListener(mEventLogger);
if (mSpeedPlaybackParameters != null) {
mInternalPlayer.setPlaybackParameters(mSpeedPlaybackParameters);
}
if (mSurface != null)
mInternalPlayer.setVideoSurface(mSurface);

mInternalPlayer.prepare(mMediaSource);
mInternalPlayer.setPlayWhenReady(false);
}
}
);
}

public void stopPlayback() {
mInternalPlayer.stop();
}
Expand Down Expand Up @@ -441,7 +449,6 @@ public int getBufferedPercentage() {
}



@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

Expand Down

0 comments on commit d2a0d37

Please sign in to comment.