Created
December 23, 2018 15:55
-
-
Save azzumw/500be8450642b82c5c11046b47981a0d to your computer and use it in GitHub Desktop.
This file contains 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
import android.net.Uri; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import com.google.android.exoplayer2.DefaultLoadControl; | |
import com.google.android.exoplayer2.DefaultRenderersFactory; | |
import com.google.android.exoplayer2.ExoPlayerFactory; | |
import com.google.android.exoplayer2.SimpleExoPlayer; | |
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; | |
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.trackselection.TrackSelector; | |
import com.google.android.exoplayer2.ui.PlayerView; | |
import com.google.android.exoplayer2.upstream.BandwidthMeter; | |
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.upstream.DefaultHttpDataSourceFactory; | |
import com.google.android.exoplayer2.util.Util; | |
public class MainActivity extends AppCompatActivity { | |
private SimpleExoPlayer player; | |
private PlayerView mPlayerView; | |
int currentWindow; | |
long playBackPosition; | |
// autoplay = false | |
private boolean autoPlay = false; | |
// constant fields for saving and restoring bundle | |
public static final String AUTOPLAY = "autoplay"; | |
public static final String CURRENT_WINDOW_INDEX = "current_window_index"; | |
public static final String PLAYBACK_POSITION = "playback_position"; | |
private final String PATH = "file:///android_asset/videoplayback.mp4"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mPlayerView = findViewById(R.id.playerview); | |
Log.e("ON_CREATE", "OnCreate"); | |
if (savedInstanceState != null) { | |
Log.e("ON_CREATE", "OnsaveInstance not null"); | |
playBackPosition = savedInstanceState.getLong(PLAYBACK_POSITION); | |
currentWindow = savedInstanceState.getInt(CURRENT_WINDOW_INDEX); | |
autoPlay = savedInstanceState.getBoolean(AUTOPLAY); | |
} | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
Log.e("ON_PAUSE", "OnPause"); | |
releasePlayer(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
Log.e("ON_RESUME", "onResume"); | |
initializePlayer(); | |
} | |
void initializePlayer(){ | |
Uri uri = Uri.parse(PATH); | |
//create exoplayer object | |
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); | |
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); | |
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); | |
player = ExoPlayerFactory.newSimpleInstance(this,trackSelector); | |
MediaSource mediaSource = buildMediaSource(uri); | |
//attach the player to the view | |
mPlayerView.setPlayer(player); | |
player.setPlayWhenReady(autoPlay); | |
player.seekTo(currentWindow,playBackPosition); | |
player.prepare(mediaSource); | |
} | |
/* | |
* This method returns ExtractorMediaSource or one of its compositions | |
* ExtractorMediaSource is suitable for playing regular files like (mp4, mp3, webm etc.) | |
* This is appropriate for the baking app project, since all recipe videos are not in adaptive formats (i.e. HLS, Dash etc) | |
*/ | |
private MediaSource buildMediaSource(Uri uri) { | |
DefaultExtractorsFactory extractorSourceFactory = new DefaultExtractorsFactory(); | |
// DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ua"); | |
String userAgent = Util.getUserAgent(this, getApplicationContext().getApplicationInfo().packageName); | |
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, userAgent); | |
// ExtractorMediaSource audioSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorSourceFactory, null, null); | |
// this return a single mediaSource object. i.e. no next, previous buttons to play next/prev media file | |
return new ExtractorMediaSource(uri, dataSourceFactory, extractorSourceFactory, null, null); | |
} | |
private void releasePlayer() { | |
if (player != null) { | |
Log.e("RELEASE PLAYER", "releasing player not null"); | |
// save the player state before releasing its resources | |
playBackPosition = player.getCurrentPosition(); | |
currentWindow = player.getCurrentWindowIndex(); | |
autoPlay = player.getPlayWhenReady(); | |
player.release(); | |
player = null; | |
Log.e("RELEASE PLAYER", " player set to null"); | |
} | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
Log.e("OnSTOP", "ON STOP"); | |
} | |
// save app state before all members are gone forever :D | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
Log.e("SVE INSTANCE", "onsaveInstanceState player not null"); | |
if(player == null){ | |
Log.e("SVE INSTANCE", "onsaveInstanceState player null"); | |
outState.putLong(PLAYBACK_POSITION, playBackPosition); | |
outState.putInt(CURRENT_WINDOW_INDEX, currentWindow); | |
outState.putBoolean(AUTOPLAY, autoPlay); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment