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 @Provides
@Named("oauth") @Named("oauth")
@Singleton @Singleton
Retrofit provideOauthRetrofit(OkHttpClient okHttpClient) { Retrofit provideOauthRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder() return new Retrofit.Builder()
.baseUrl(APIUtils.OAUTH_API_BASE_URI) .baseUrl(APIUtils.OAUTH_API_BASE_URI)
.client(okHttpClient) .client(okHttpClient)
@ -155,7 +155,7 @@ class AppModule {
@Provides @Provides
@Named("strapi") @Named("strapi")
@Singleton @Singleton
Retrofit providestrapiRetrofit(OkHttpClient okHttpClient) { Retrofit providestrapiRetrofit(@Named("default") OkHttpClient okHttpClient) {
return new Retrofit.Builder() return new Retrofit.Builder()
.baseUrl(APIUtils.STRAPI_BASE_URI) .baseUrl(APIUtils.STRAPI_BASE_URI)
.client(okHttpClient) .client(okHttpClient)
@ -164,6 +164,7 @@ class AppModule {
} }
@Provides @Provides
@Named("default")
@Singleton @Singleton
OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase,
@Named("current_account") SharedPreferences currentAccountSharedPreferences) { @Named("current_account") SharedPreferences currentAccountSharedPreferences) {
@ -176,6 +177,17 @@ class AppModule {
return okHttpClientBuilder.build(); 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 @Provides
@Singleton @Singleton
RedditDataRoomDatabase provideRedditDataRoomDatabase() { RedditDataRoomDatabase provideRedditDataRoomDatabase() {

View File

@ -40,7 +40,6 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -85,6 +84,9 @@ public class ViewRPANBroadcastFragment extends Fragment {
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Inject @Inject
Executor mExecutor; Executor mExecutor;
@Inject
@Named("rpan")
OkHttpClient okHttpClient;
private AppCompatActivity mActivity; private AppCompatActivity mActivity;
private RPANBroadcast rpanBroadcast; private RPANBroadcast rpanBroadcast;
private SimpleExoPlayer player; private SimpleExoPlayer player;
@ -92,6 +94,7 @@ public class ViewRPANBroadcastFragment extends Fragment {
private DataSource.Factory dataSourceFactory; private DataSource.Factory dataSourceFactory;
private Handler handler; private Handler handler;
private RPANCommentStreamRecyclerViewAdapter adapter; private RPANCommentStreamRecyclerViewAdapter adapter;
private WebSocket webSocket;
private boolean wasPlaying; private boolean wasPlaying;
private boolean isMute = false; private boolean isMute = false;
@ -152,13 +155,6 @@ public class ViewRPANBroadcastFragment extends Fragment {
player = ExoPlayerFactory.newSimpleInstance(mActivity, trackSelector); player = ExoPlayerFactory.newSimpleInstance(mActivity, trackSelector);
playerView.setPlayer(player); 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; wasPlaying = true;
boolean muteVideo = mSharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_VIDEO, false) || boolean muteVideo = mSharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_VIDEO, false) ||
@ -229,16 +225,6 @@ public class ViewRPANBroadcastFragment extends Fragment {
handler = new Handler(); 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; return rootView;
} }
@ -299,9 +285,26 @@ public class ViewRPANBroadcastFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.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) { if (wasPlaying) {
player.setPlayWhenReady(true); 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 @Override