I'am developing the Audio Streaming Android App
. I used foreground service
for that. it is working perfectly but in some phone my app crash when stop the media player
.
I Have check MediaPlayer Service
is null
before stop the service. every time it is true in some mobile phones but music is playing in background. so i remove null check. then application automatically close because of above issue.
public class MainActivity extends AppCompatActivity implements MediaPlayerService.ServiceCallbacks {
final String TAG = MainActivity.class.getSimpleName();
ImageButton btnPlay_Puse;
TextView loadingImage;
boolean mBounded;
FirebaseAnalytics mFirebaseAnalytics;
MediaPlayerService mMediaPlayerService;
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName componentName) {
MainActivity mainActivity = MainActivity.this;
mainActivity.mBounded = false;
mainActivity.mMediaPlayerService = null;
}
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
MainActivity mainActivity = MainActivity.this;
mainActivity.mBounded = true;
mainActivity.mMediaPlayerService = ((MediaPlayerService.LocalBinder) iBinder).getServerInstance();
MainActivity.this.mMediaPlayerService.setCallbacks(MainActivity.this);
}
};
PowerManager.WakeLock wakeLock;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
requestWindowFeature(1);
Objects.requireNonNull(getSupportActionBar()).hide();
getWindow().setFlags(1024, 1024);
setContentView(R.layout.activity_main);
this.wakeLock = ((PowerManager) Objects.requireNonNull(getSystemService(POWER_SERVICE)))
.newWakeLock(1, "MyApp::MyWakelockTag");
try {
if (!isMyServiceRunning(MediaPlayerService.class)) {
Intent intent = new Intent(this, MediaPlayerService.class);
intent.setAction("action.PLAY");
bindService(intent, this.mConnection, Context.BIND_AUTO_CREATE);
startService(intent);
return;
}
StartRadioPlayer_();
} catch (Exception e) {
String sb = "onCreate: " + e.toString();
Log.e(this.TAG, sb);
}
}
public void onStop() {
super.onStop();
if (this.mBounded) {
unbindService(this.mConnection);
this.mBounded = false;
}
}
public void callFromService() {
StartRadioPlayer_();
}
public void onClickPlayPuse(View view) {
MediaPlayerService mediaPlayerService = this.mMediaPlayerService;
if (mediaPlayerService == null || !mediaPlayerService.isPlaying()) {
StartRadioPlayer();
return;
}
StopRadioPlayer();
}
public void StartRadioPlayer_() {
this.loadingImage.setVisibility(View.INVISIBLE);
this.btnPlay_Puse.setVisibility(View.VISIBLE);
this.btnPlay_Puse.setBackgroundResource(R.drawable.pause);
this.btnPlay_Puse.setTag("pause");
}
public void StartRadioPlayer() {
MediaPlayerService mediaPlayerService = this.mMediaPlayerService;
if (mediaPlayerService != null) {
mediaPlayerService.startMediaPlayer();
}
}
public void StopRadioPlayer() {
MediaPlayerService mediaPlayerService = this.mMediaPlayerService;
mediaPlayerService.stopMediaPlayer();
}
public boolean isInternetConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = Objects.requireNonNull(cm).getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
private boolean isMyServiceRunning(Class<?> cls) {
for (ActivityManager.RunningServiceInfo runningServiceInfo : ((ActivityManager) Objects.requireNonNull(getSystemService(ACTIVITY_SERVICE)))
.getRunningServices(Integer.MAX_VALUE)) {
if (cls.getName().equals(runningServiceInfo.service.getClassName())) {
return true;
}
}
return false;
}
I expect Media player Stop without any error as below.
com.name.name.MainActivity.StopRadioPlayer (MainActivity.java:189)
com.name.name.MainActivity.onClickPlayPuse (MainActivity.java:162)
java.lang.reflect.Method.invoke (Method.java)
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick (AppCompatViewInflater.java:385)
android.view.View.performClick (View.java:7341)
android.view.View.performClickInternal (View.java:7307)
android.view.View.access$3200 (View.java:846)
android.view.View$PerformClick.run (View.java:27789)
android.os.Handler.handleCallback (Handler.java:873)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:214)
android.app.ActivityThread.main (ActivityThread.java:7097)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:975)