Created
February 9, 2018 15:48
-
-
Save enginebai/73a693ea038112d53c5d3ea9ec867165 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.machipopo.swag.utils; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.os.Handler; | |
import com.google.android.exoplayer2.ExoPlayerFactory; | |
import com.google.android.exoplayer2.Player; | |
import com.google.android.exoplayer2.SimpleExoPlayer; | |
import com.google.android.exoplayer2.source.MediaSource; | |
import com.google.android.exoplayer2.source.dash.DashMediaSource; | |
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource; | |
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; | |
import com.google.android.exoplayer2.trackselection.FixedTrackSelection; | |
import com.google.android.exoplayer2.upstream.HttpDataSource; | |
import com.machipopo.swag.GlobalContext; | |
import com.machipopo.swag.Injection; | |
import com.machipopo.swag.data.api.ApiHelper; | |
import okhttp3.OkHttpClient; | |
/** | |
* Created by dannylin on 2017/12/23. | |
*/ | |
public class MediaPlayerController { | |
private Context mContext; | |
private Handler mMainHandler; | |
private SimpleExoPlayer mPlayer; | |
// private DataSource.Factory mManifestDataSourceFactory; | |
// private DataSource.Factory mChunkDataSourceFactory; | |
private DefaultTrackSelector mTrackSelector; | |
private PlayerEventLogger mEventLogger; | |
private Player.EventListener mPlayerListener; | |
private final OkHttpClient mHttpClient; | |
private boolean mStartPlay = false; | |
private HttpDataSource.Factory mDataSource; | |
public MediaPlayerController(Context context) { | |
mContext = context; | |
mMainHandler = new Handler(); | |
mTrackSelector = new DefaultTrackSelector( | |
new FixedTrackSelection.Factory()); | |
mHttpClient = GlobalContext.getPlayerHttpClientInstance(context); | |
mDataSource = MediaUtils.buildHttpDataSourceFactory( | |
mHttpClient, | |
ApiHelper.getUserAgent(mContext), | |
Injection.provideRepository(mContext).getJwt(), | |
MediaUtils.BANDWIDTH_METER); | |
mEventLogger = new PlayerEventLogger(mTrackSelector); | |
mPlayer = ExoPlayerFactory.newSimpleInstance(context, mTrackSelector); | |
mPlayer.setPlayWhenReady(false); | |
mPlayer.setRepeatMode(Player.REPEAT_MODE_ALL); | |
if (((GlobalContext)context.getApplicationContext()).isDebugMode()) | |
mPlayer.addListener(mEventLogger); | |
} | |
public void preparePlayer(String jwt, String dashUrl) { | |
HttpDataSource.Factory sourceFactory = MediaUtils.buildHttpDataSourceFactory( | |
mHttpClient, | |
ApiHelper.getUserAgent(mContext), | |
jwt, | |
MediaUtils.BANDWIDTH_METER); | |
MediaSource videoSource = new DashMediaSource(Uri.parse(dashUrl), mDataSource, | |
new DefaultDashChunkSource.Factory(sourceFactory), | |
mMainHandler, | |
null); | |
mPlayer.prepare(videoSource); | |
} | |
public void release() { | |
if (mPlayer != null) { | |
removePlayerListener(); | |
mPlayer.clearVideoSurface(); | |
mPlayer.release(); | |
mPlayer = null; | |
} | |
mTrackSelector = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment