当我播放音频文件时,我的应用程序不断停止工作。 (科特林)

问题描述 投票:0回答:1

进程:com.example.musicplayer,PID:5970 android.app.MissingForegroundServiceTypeException:在没有类型的情况下启动 FGS callerApp=ProcessRecord{e34753a 5970:com.example.musicplayer/u0a192} targetSDK=34 在 android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:53) 在 android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:49) 在 android.os.Parcel.readParcelableInternal(Parcel.java:4870) 在 android.os.Parcel.readParcelable(Parcel.java:4852) 在 android.os.Parcel.createExceptionOrNull(Parcel.java:3052) 在 android.os.Parcel.createException(Parcel.java:3041) 在 android.os.Parcel.readException(Parcel.java:3024) 在 android.os.Parcel.readException(Parcel.java:2966) 在 android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761) 在 android.app.Service.startForeground(Service.java:775) 在 com.example.musicplayer.MusicService.showNotification(MusicService.kt:39) 在 com.example.musicplayer.PlayerActivity.onServiceConnected(PlayerActivity.kt:128) 在 android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2198) 在 android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2231) 在 android.os.Handler.handleCallback(Handler.java:958) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loopOnce(Looper.java:205) 在 android.os.Looper.loop(Looper.java:294) 在 android.app.ActivityThread.main(ActivityThread.java:8177) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

[Music Service(2)](https://i.stack.imgur.com/FF7Pa.png)

Music Service(1) 剩余代码:- .build() startForeground(13,通知) ApplicationClass

kotlin audio-player
1个回答
0
投票

如果targetSdkVersion为34,则为新功能。

https://developer.android.com/develop/background-work/services/foreground-services?hl=zh-cn#types

你应该做:

  1. 添加前台服务类型
<service
    android:name=".MyMediaPlaybackService"
    android:foregroundServiceType="mediaPlayback"
    android:exported="false">

  1. 添加 FOREGROUND_SERVICE_MEDIA_PLAYBACK https://developer.android.com/reference/android/Manifest.permission#FOREGROUND_SERVICE_MEDIA_PLAYBACK
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
  1. 尝试从类型开始:
 ServiceCompat.startForeground(
    /* service = */ this,
    /* id = */ 100, // Cannot be 0
    /* notification = */ notification,
    /* foregroundServiceType = */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        ServiceInfo.FOREGROUND_SERVICE_MEDIA_PLAYBACK
    } else {
        0
    },
)

有关 Android 14 更改的更多信息:

https://developer.android.com/about/versions/14/changes/fgs-types-required

© www.soinside.com 2019 - 2024. All rights reserved.