move player back into fragment

This commit is contained in:
Felix 2017-10-01 20:52:30 +02:00
parent 8ce8b3bf9e
commit 8a5cd4c35b
2 changed files with 20 additions and 32 deletions

View file

@ -34,7 +34,6 @@ public class ChaosflixViewModel extends ViewModel {
private static final String TAG = ChaosflixViewModel.class.getSimpleName(); private static final String TAG = ChaosflixViewModel.class.getSimpleName();
private final StreamingService mStreamingApi; private final StreamingService mStreamingApi;
private final RecordingService mRecordingApi; private final RecordingService mRecordingApi;
private SimpleExoPlayer exoPlayer;
public ChaosflixViewModel(String recordingUrl, String streamingUrl){ public ChaosflixViewModel(String recordingUrl, String streamingUrl){
OkHttpClient client = new OkHttpClient(); OkHttpClient client = new OkHttpClient();
@ -118,14 +117,6 @@ public class ChaosflixViewModel extends ViewModel {
}).subscribeOn(Schedulers.io()); }).subscribeOn(Schedulers.io());
} }
public SimpleExoPlayer getExoPlayer(){
return exoPlayer;
}
public void setExoPlayer(SimpleExoPlayer exoPlayer) {
this.exoPlayer = exoPlayer;
}
public static class Factory extends ViewModelProvider.NewInstanceFactory{ public static class Factory extends ViewModelProvider.NewInstanceFactory{
private final String recordingUrl; private final String recordingUrl;

View file

@ -72,6 +72,7 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
private boolean mPlaybackState = true; private boolean mPlaybackState = true;
private Event mEvent; private Event mEvent;
private Recording mRecording; private Recording mRecording;
private SimpleExoPlayer exoPlayer;
public ExoPlayerFragment() { public ExoPlayerFragment() {
} }
@ -112,12 +113,10 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
if(subtitleText != null) if(subtitleText != null)
subtitleText.setText(mEvent.getSubtitle()); subtitleText.setText(mEvent.getSubtitle());
SimpleExoPlayer player; if(exoPlayer == null){
if(getViewModel().getExoPlayer() == null){ exoPlayer = setupPlayer();
player = setupPlayer();
getViewModel().setExoPlayer(player);
} else { } else {
player = getViewModel().getExoPlayer(); exoPlayer = exoPlayer;
Log.d(TAG,"Player already set up."); Log.d(TAG,"Player already set up.");
} }
@ -126,13 +125,12 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
SimpleExoPlayer player = getViewModel().getExoPlayer(); if(exoPlayer != null){
if(player != null){ exoPlayer.setPlayWhenReady(mPlaybackState);
player.setPlayWhenReady(mPlaybackState);
getViewModel().getPlaybackProgress(mEvent.getApiID()) getViewModel().getPlaybackProgress(mEvent.getApiID())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong -> player.seekTo(aLong)); .subscribe(aLong -> exoPlayer.seekTo(aLong));
videoView.setPlayer(player); videoView.setPlayer(exoPlayer);
} }
} }
@ -146,10 +144,9 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
SimpleExoPlayer player = getViewModel().getExoPlayer(); if(exoPlayer != null){
if(player != null){ getViewModel().setPlaybackProgress(mEvent.getApiID(), exoPlayer.getCurrentPosition());
getViewModel().setPlaybackProgress(mEvent.getApiID(), player.getCurrentPosition()); exoPlayer.setPlayWhenReady(false);
player.setPlayWhenReady(false);
} }
} }
@ -163,8 +160,8 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if(getViewModel().getExoPlayer() != null){ if(exoPlayer != null){
outState.putBoolean(PLAYBACK_STATE, getViewModel().getExoPlayer().getPlayWhenReady()); outState.putBoolean(PLAYBACK_STATE, exoPlayer.getPlayWhenReady());
} }
} }
@ -182,15 +179,15 @@ public class ExoPlayerFragment extends ChaosflixFragment implements MyListener.P
= new DefaultRenderersFactory(getContext(), null, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF); = new DefaultRenderersFactory(getContext(), null, DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl); exoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl);
MyListener listener = new MyListener(player, this); MyListener listener = new MyListener(exoPlayer, this);
player.addVideoListener(listener); exoPlayer.addVideoListener(listener);
player.addListener(listener); exoPlayer.addListener(listener);
player.setPlayWhenReady(mPlaybackState); exoPlayer.setPlayWhenReady(mPlaybackState);
player.prepare(buildMediaSource(Uri.parse(mRecording.getRecordingUrl()),"")); exoPlayer.prepare(buildMediaSource(Uri.parse(mRecording.getRecordingUrl()),""));
return player; return exoPlayer;
} }
@Override @Override