[Crash] : avoid crash when leaving the application

This commit is contained in:
Olivier Bouillet 2020-09-25 21:56:09 +02:00
parent ae30ab708e
commit cdcfa8c28e

View File

@ -113,8 +113,7 @@ public class VideoPlayerService extends Service {
if (playbackState
== ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped
Log.v(TAG, "ACTION_PAUSE: " + playbackState);
unregisterReceiver(myNoisyAudioStreamReceiver);
myNoisyAudioStreamReceiver = null;
safeUnregisterReceiver();
}
}
});
@ -137,14 +136,9 @@ public class VideoPlayerService extends Service {
if (playerNotificationManager != null) {
playerNotificationManager.setPlayer(null);
}
//Was seeing an error when exiting the program about about not unregistering the receiver.
try {
if (null != myNoisyAudioStreamReceiver) {
this.unregisterReceiver(myNoisyAudioStreamReceiver);
}
} catch (Exception e) {
Log.e("VideoPlayerService", "attempted to unregister a nonregistered service");
}
//Was seeing an error when exiting the program about not unregistering the receiver.
safeUnregisterReceiver();
if (player != null) {
player.release();
player = null;
@ -152,6 +146,15 @@ public class VideoPlayerService extends Service {
super.onDestroy();
}
private void safeUnregisterReceiver()
{
try {
unregisterReceiver(myNoisyAudioStreamReceiver);
} catch (Exception e) {
Log.e("VideoPlayerService", "attempted to unregister a nonregistered service");
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {