简体   繁体   English

如何在屏幕关闭后保持播放有效

[英]How to keep playback active after the screen is turned off

I have an app with audio playback, but on some devices (mainly Huawei, Samsung, Sony) playback is stopped when the screen goes off. 我有一个带音频播放的应用程序,但在某些设备(主要是华为,三星,索尼)上,当屏幕熄灭时播放停止。 Note: playback does not stop for some other apps like Spotify, Play Music 注意:对于Spotify,Play Music等其他应用,播放不会停止

I found this answer (for Huawei devices) which suggest to ask user to enable "run in background" permission in battery optimization settings (Battery Manager > Protected Apps) but I would like to avoid asking users to enable it. 我发现这个答案(对于华为设备)建议要求用户在电池优化设置(电池管理器>受保护的应用程序)中启用“后台运行”权限,但我想避免要求用户启用它。

Is there some API to enable it programmatically for Huawei, Samsung, Sony or other manufacturers? 是否有一些API可以为华为,三星,索尼或其他制造商提供编程方式?

Is it possible to detect if app do not have such permission? 是否可以检测应用程序是否没有此类权限?

EDIT: playback runs in foreground service and app holds wakelock 编辑:播放在前台服务运行,应用程序持有唤醒锁

EDIT 2: I've contacted Huawei support related to this issue. 编辑2:我已经联系了与此问题相关的华为支持。 I will share more info once resolved. 一旦解决,我将分享更多信息。

Use a service with foreground priority the newer Android OS versions have a doze function to save battery. 使用具有前台优先级服务,较新的Android OS版本具有打盹功能以节省电池电量。

If you already use the service in foreground probably there is something wrong with the way you use the service in your code (give more details). 如果您已在前台使用该服务,则可能是您在代码中使用该服务的方式有问题(提供更多详细信息)。

Here a tutorial about Playing music in the background that could help you. 这里有一个关于在后台播放可以帮助你的音乐的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM