首页 > 解决方案 > Kotlin Android 10 的通知中没有自定义声音

问题描述

我试图播放自定义声音文件作为通知,但手机只播放默认声音文件。(Kotlin)我已经卸载并安装了该应用程序。代码应该没问题。该应用程序是一个倒计时到零的计时器。-> 现在零应该是一个声音(原始文件),我的代码:首先是我的函数。然后是 BasicNotificationBuilder,然后是 PendingIntentWithStack,最后是带有 createNotificationChannel 的 NotificationManager。我发现的都是Java。Kotlin 的解决方案非常适合运行声音文件 谢谢

 fun showTimerExpired(context: Context) { 
        val startIntent = Intent(context, TimerNotificationActionReceiver::class.java)
        startIntent.action = Constants.ACTION_START
        val startPendingIntent = PendingIntent.getBroadcast(
            context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT)

        val soundUri:Uri = Uri.parse(
            "android.resource://" + context.packageName.toString()
                    + "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)

        val notificationBuilder =
            getBasicNotificationBuilder(context, CHANNEL_ID, true) 
        notificationBuilder.setContentTitle("Timer Expired")
            .setContentText("Fertig")
            .setContentIntent(getPendingIntentWithStack(context, MainActivity::class.java))
            .addAction(q7.com.eieruhrbackforenot.R.drawable.ic_start, "Start", startPendingIntent)
            .setSound(soundUri, AudioManager.STREAM_NOTIFICATION)
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.createNotificationChannel(
            CHANNEL_ID, NAME_CHANNEL_ID, true
        )
        notificationManager.notify(TIMER_ID, notificationBuilder.build())
    }
    private fun getBasicNotificationBuilder(
        context: Context, channelID: String, playSound: Boolean
    ): NotificationCompat.Builder {
        val soundUri:Uri = Uri.parse(
            "android.resource://" + context.packageName.toString()
                    + "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)
        val notificationBuilder = NotificationCompat.Builder(context, channelID)
            .setSmallIcon(q7.com.eieruhrbackforenot.R.drawable.ic_timer)
            .setAutoCancel(true)// User drückt Notification zum schliessen
            .setDefaults(0)// keine Defaults
            .setSound(soundUri)
        if (playSound) {
            notificationBuilder.setSound(soundUri)
            //notificationBuilder.setSound(notificationSoundUri)
        }
        return notificationBuilder
    }
    private fun <T> getPendingIntentWithStack(
        context: Context,
        javaClass: Class<T>
    ): PendingIntent {
        val soundUri:Uri = Uri.parse(
            "android.resource://" + context.packageName.toString()
                    + "/" + q7.com.eieruhrbackforenot.R.raw.i_feel_good)
        val resultIntent = Intent(context, javaClass)
        resultIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
        val stackBuilder = TaskStackBuilder.create(context)
        stackBuilder.addParentStack(javaClass)
        stackBuilder.addNextIntent(resultIntent)
        return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
    }
    private fun NotificationManager.createNotificationChannel(
        channelID: String,
        channelName: String,
        playSound: Boolean
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelImportance = if (playSound) {
                NotificationManager.IMPORTANCE_DEFAULT
            } else {
                NotificationManager.IMPORTANCE_LOW
            }
            val notificationChannel =
                NotificationChannel(channelID, channelName, channelImportance)
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.CYAN
            notificationChannel.enableVibration(true)
            notificationChannel.vibrationPattern = longArrayOf(100,200,300,400,500,400,300,200,400)
            this.createNotificationChannel(notificationChannel)
            }
        }
    }
}

标签: kotlinaudiotimernotificationsforeground

解决方案


而不是将声音更改为 mp3 文件。我创建了一个带有未决意图的意图,它在计时器结束时开始播放音乐。它有效,但这不是我想要的。

 fun showTimerIsExpired(context: Context) { 
            val playMusicIntent = Intent(context,MainActivity::class.java)
            playMusicIntent.action = MainActivity.musicStart().toString()
            val playMusicPendingIntent = PendingIntent.getBroadcast(
                context,0,playMusicIntent,PendingIntent.FLAG_UPDATE_CURRENT)

并将 pendingIntent 设置为 .ContentIntent-> .setContentIntent(playMusicPendingIntent) ;在我的情况下为 addAction-> .addAction(R.drawable.ic_start, "Start", playMusicPendingIntent)


推荐阅读