Initialize player and websocket connection in onResume in ViewRPANBroadcastFragment.

This commit is contained in:
Alex Ning 2021-07-06 23:40:37 +08:00
parent 236b6b66f1
commit ac72a30458
2 changed files with 35 additions and 20 deletions

View File

@ -44,7 +44,7 @@ class AppModule {
@Provides
@Named("oauth")
@Singleton
Retrofit provideOauthRetrofit(OkHttpClient okHttpClient) {
Retrofit provideOauthRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
.client(okHttpClient)
@ -155,7 +155,7 @@ class AppModule {
@Provides
@Named("strapi")
@Singleton
Retrofit providestrapiRetrofit(OkHttpClient okHttpClient) {
Retrofit providestrapiRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIUtils.STRAPI_BASE_URI)
.client(okHttpClient)
@ -164,6 +164,7 @@ class AppModule {
}
@Provides
@Named("default")
@Singleton
OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase,
@Named("current_account") SharedPreferences currentAccountSharedPreferences) {
@ -176,6 +177,17 @@ class AppModule {
return okHttpClientBuilder.build();
}
@Provides
@Named("rpan")
@Singleton
OkHttpClient provideRPANOkHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
}
@Provides
@Singleton
RedditDataRoomDatabase provideRedditDataRoomDatabase() {

View File

@ -40,7 +40,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
@ -85,6 +84,9 @@ public class ViewRPANBroadcastFragment extends Fragment {
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
@Inject
@Named("rpan")
OkHttpClient okHttpClient;
private AppCompatActivity mActivity;
private RPANBroadcast rpanBroadcast;
private SimpleExoPlayer player;
@ -92,6 +94,7 @@ public class ViewRPANBroadcastFragment extends Fragment {
private DataSource.Factory dataSourceFactory;
private Handler handler;
private RPANCommentStreamRecyclerViewAdapter adapter;
private WebSocket webSocket;
private boolean wasPlaying;
private boolean isMute = false;
@ -152,13 +155,6 @@ public class ViewRPANBroadcastFragment extends Fragment {
player = ExoPlayerFactory.newSimpleInstance(mActivity, trackSelector);
playerView.setPlayer(player);
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(mActivity, "Infinity"));
// Prepare the player with the source.
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(rpanBroadcast.rpanStream.hlsUrl)));
player.setRepeatMode(Player.REPEAT_MODE_ALL);
if (resumePosition > 0) {
player.seekTo(resumePosition);
}
wasPlaying = true;
boolean muteVideo = mSharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_VIDEO, false) ||
@ -229,16 +225,6 @@ public class ViewRPANBroadcastFragment extends Fragment {
handler = new Handler();
Request request = new Request.Builder().url(rpanBroadcast.rpanPost.liveCommentsWebsocketUrl).build();
CommentStreamWebSocketListener listener = new CommentStreamWebSocketListener(this::parseComment);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
WebSocket webSocket = okHttpClient.newWebSocket(request, listener);
okHttpClient.dispatcher().executorService().shutdown();
return rootView;
}
@ -299,9 +285,26 @@ public class ViewRPANBroadcastFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
if (dataSourceFactory == null) {
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(mActivity, "Infinity"));
// Prepare the player with the source.
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(rpanBroadcast.rpanStream.hlsUrl)));
player.setRepeatMode(Player.REPEAT_MODE_ALL);
if (resumePosition > 0) {
player.seekTo(resumePosition);
}
}
if (wasPlaying) {
player.setPlayWhenReady(true);
}
if (webSocket == null) {
Request request = new Request.Builder().url(rpanBroadcast.rpanPost.liveCommentsWebsocketUrl).build();
CommentStreamWebSocketListener listener = new CommentStreamWebSocketListener(this::parseComment);
webSocket = okHttpClient.newWebSocket(request, listener);
okHttpClient.dispatcher().executorService().shutdown();
}
}
@Override