From ac72a30458ca6e2557abedbd1ba2247dd5c366eb Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 6 Jul 2021 23:40:37 +0800 Subject: Initialize player and websocket connection in onResume in ViewRPANBroadcastFragment. --- .../infinityforreddit/AppModule.java | 16 +++++++-- .../fragments/ViewRPANBroadcastFragment.java | 39 ++++++++++++---------- 2 files changed, 35 insertions(+), 20 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index b539fc89..435969a0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java index 5fa26888..bc3d92ae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRPANBroadcastFragment.java @@ -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 -- cgit v1.2.3