diff --git a/android/app/src/main/java/tf/monochrome/music/AudioPlaybackService.java b/android/app/src/main/java/tf/monochrome/music/AudioPlaybackService.java index 8fbc5c0..e951449 100644 --- a/android/app/src/main/java/tf/monochrome/music/AudioPlaybackService.java +++ b/android/app/src/main/java/tf/monochrome/music/AudioPlaybackService.java @@ -33,6 +33,11 @@ public class AudioPlaybackService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + if (intent != null && "STOP".equals(intent.getAction())) { + stopSelf(); + return START_NOT_STICKY; + } + Notification notification = buildNotification(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -95,6 +100,9 @@ public class AudioPlaybackService extends Service { } private void acquireWakeLock() { + if (wakeLock != null && !wakeLock.isHeld()) { + wakeLock = null; + } if (wakeLock == null) { PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); if (pm != null) { diff --git a/android/app/src/main/java/tf/monochrome/music/BackgroundAudioPlugin.java b/android/app/src/main/java/tf/monochrome/music/BackgroundAudioPlugin.java index 6588d6a..afb5d76 100644 --- a/android/app/src/main/java/tf/monochrome/music/BackgroundAudioPlugin.java +++ b/android/app/src/main/java/tf/monochrome/music/BackgroundAudioPlugin.java @@ -18,18 +18,24 @@ public class BackgroundAudioPlugin extends Plugin { @PluginMethod public void start(PluginCall call) { - Intent intent = new Intent(getContext(), AudioPlaybackService.class); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - getContext().startForegroundService(intent); - } else { - getContext().startService(intent); + try { + Intent intent = new Intent(getContext(), AudioPlaybackService.class); + intent.setAction("START"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + getContext().startForegroundService(intent); + } else { + getContext().startService(intent); + } + call.resolve(); + } catch (Exception e) { + call.reject("Failed to start audio service: " + e.getMessage(), e); } - call.resolve(); } @PluginMethod public void stop(PluginCall call) { Intent intent = new Intent(getContext(), AudioPlaybackService.class); + intent.setAction("STOP"); getContext().stopService(intent); call.resolve(); } diff --git a/index.html b/index.html index 111c115..f1bb5d7 100644 --- a/index.html +++ b/index.html @@ -4259,7 +4259,7 @@