Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Mode Not Working #11

Open
sirgatsen opened this issue May 19, 2023 · 17 comments
Open

Background Mode Not Working #11

sirgatsen opened this issue May 19, 2023 · 17 comments
Assignees
Labels
bug Something isn't working

Comments

@sirgatsen
Copy link

My app uses the standard HTML5 Audio element to play audio and Android keeps killing my app after a few (3-4) minutes in the background. My test device is on Android 11

I know you are using a foreground service to provide this functionality but can you maybe look into adding the wake lock if you aren't already?

@fromage9747
Copy link

@sirgatsen Try adding
<uses-permission android:name="android.permission.BACKGROUND_SERVICE" />
to your AndroidManifest.xml

@sirgatsen
Copy link
Author

@fromage9747 you mean
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
I have that one in and I also have
<uses-permission android:name="android.permission.WAKE_LOCK" />

@webmasterab
Copy link

webmasterab commented May 19, 2023

If you use this plugin it will no longer run in the background but in the foreground and will not stop if the app goes to the background.

Recently had it myself and solved it with this plugin

You can then make it like this
Create a variable audio: any;
Then in the constructor I have this.audio = new Audio;
Then I have functions
play() {
this.audio.src = 'link to file or stream';
this.audio.play();
}

I have inside the play() {}
The code of this plugin.

@fromage9747
Copy link

If you use this plugin it will no longer run in the background but in the foreground and will not stop if the app goes to the background.

Recently had it myself and solved it with this plugin

You can then make it like this Create a variable audio: any; Then in the constructor I have this.audio = new Audio; Then I have functions play() { this.audio.src = 'link to file or stream'; this.audio.play(); }

I have inside the play() {} The code of this plugin.

That's pretty much what I have in mine. So far so good 👍

@sirgatsen
Copy link
Author

If you use this plugin it will no longer run in the background but in the foreground and will not stop if the app goes to the background.

Recently had it myself and solved it with this plugin

You can then make it like this Create a variable audio: any; Then in the constructor I have this.audio = new Audio; Then I have functions play() { this.audio.src = 'link to file or stream'; this.audio.play(); }

I have inside the play() {} The code of this plugin.

That is also pretty much my setup as well, but like I said my Android app dies randomly after a few varied minutes.

@ArielAleksandrus
Copy link

ArielAleksandrus commented Jun 15, 2023

Anyone solved this issue? Mine plays a few minutes then die. I'm using Native Media . Also, my media controls are showing in the notifications, but not when phone is locked. And I can't skip or go to previous from my car bluetooth

@jofr jofr added the bug Something isn't working label Jun 18, 2023
@jofr jofr self-assigned this Jun 18, 2023
@Amoyens1s
Copy link

Anyone solved this issue? Mine plays a few minutes then die. I'm using Native Media . Also, my media controls are showing in the notifications, but not when phone is locked. And I can't skip or go to previous from my car bluetooth

@@ -3,6 +3,7 @@ package com.getcapacitor;
 import android.content.Context;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
+import android.view.View;
 import android.view.inputmethod.BaseInputConnection;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputConnection;
@@ -49,4 +50,9 @@ public class CapacitorWebView extends WebView {
         }
         return super.dispatchKeyEvent(event);
     }
+
+    @Override
+    public void dispatchWindowVisibilityChanged(int visibility) {
+      super.onWindowVisibilityChanged(View.VISIBLE);
+    }
 }

I override the dispatchWindowVisibilityChanged in CapacitorWebView。 now it can play video and audio in background even lock the phone. It's a trick which can let webview stay visible in background with out interrupt. I don't know whether this is a good solution, but, it works.

Hope this can solve the problem your meet.

@bonomite
Copy link

@Amoyens1s I am desperate. I have been trying to fix this background audio issue forever. I am attempting to incorporate your fix above, but am not having any luck. I beg for your guidance.

@Amoyens1s
Copy link

Amoyens1s commented Jun 13, 2024

@bonomite
My application scenario is video on demand, using the <video> tag to embed mp4 video playback. I want the video to continue playing when the app switches to the background. Normally, switching to the background pauses the webview, so the video also pauses. My method is to make the webview still "visible" to prevent it from being paused, thus allowing background playback.

I'm not sure if your audio application scenario is similar, but if you're using the <audio> tag, I think it might work?

By the way, I'm using Capacitor 5 and using patch-package to save my modifications to Capacitor.

If you have any further questions, feel free to reply to me.

image image

@bonomite
Copy link

Ah ok... I have added that code to that file... testing it now... it takes 5 minutes when the screen is off for the audio to stop playing.

@bonomite
Copy link

This is nuts... it's working! You have NO idea how long I've been trying to figure this out. So there is no way to add this change on the front end? It has to be inside the Capacitor package?

@Amoyens1s
Copy link

This is nuts... it's working! You have NO idea how long I've been trying to figure this out. So there is no way to add this change on the front end? It has to be inside the Capacitor package?

Yes, it has to be done inside the Capacitor package. There is no way to achieve this through front-end techniques.

@bonomite
Copy link

@Amoyens1s do you have a PayPal or venmo account?

@Amoyens1s
Copy link

@Amoyens1s do you have a PayPal or venmo account?

You don't need to pay me. I encountered the same problem and struggled a lot before finally solving it. Being able to help others is my greatest joy. I'm just happy I could solve your problem.

@bonomite
Copy link

I'd like to buy you a drink at least.

@fromage9747
Copy link

Just adding this here for copy and paste purposes. I didn't ever have an issue with my audio playing in the background, it just worked, but what I did have an issue with was coming back to my app I would need the screen to be touched in order for it to display and update the track in the UI that is currently playing. Haven't tested this yet, but I hope that this code sorts that.

@Override
    public void dispatchWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(View.VISIBLE);
    }

@ArielAleksandrus
Copy link

This is nuts... it's working! You have NO idea how long I've been trying to figure this out. So there is no way to add this change on the front end? It has to be inside the Capacitor package?

you have said before that after 5 minutes audio would stop working. did it get fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants