如何禁用通知声音?

时间:2020-10-27 12:04:47

标签: android

我通过RecordServicesuspend fun startRecord(filename:String){...}中录制音频,您可以在代码A中看到它。

当我调用startForegroundService()时会触发

fun startRecord(filename:String)

我需要启动前台服务时必须显示通知。

我发现显示通知时会播放声音。显示通知时如何禁用声音?

顺便说一句,通知的声音是延迟的,因此它将被mRecorder记录,因此我希望在显示通知时禁用声音。

还有,我希望mRecorder不要记录通知的声音,还有更好的方法吗?

代码A

class RecordService : Service() {

    val CHANNEL_ID = "My Notification"

    private var mRecorder: MediaRecorder? = null

    private val mBinder: IBinder = MyBinder()

    override fun onBind(intent: Intent): IBinder? {
        return mBinder
    }

    inner class MyBinder : Binder() {
        val service: RecordService
            get() = this@RecordService
    }

    override fun onCreate() {
        super.onCreate()
    }

    private fun startForegroundService() {
        val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel(
                CHANNEL_ID,
                "My Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
        } else {
            TODO("VERSION.SDK_INT < O")
        }

        (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(
            channel
        )

        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("A service is running in the background")
            .setContentText("Generating random number").build()
        startForeground(1, notification)
    }


    private fun stopForegroundService() {
        stopForeground(true)
    }


    suspend fun startRecord(filename:String){

        startForegroundService()

        mRecorder = MediaRecorder()

        withContext(Dispatchers.IO) {
            mRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            mRecorder?.setOutputFile(filename);

            mRecorder?.setMaxDuration(1000*60*20); //20 Mins
            mRecorder?.setAudioChannels(1);
            mRecorder?.setAudioSamplingRate(44100);
            mRecorder?.setAudioEncodingBitRate(192000);

            mRecorder?.prepare()
            mRecorder?.start()
        }
    }


    fun stopRecord(){
        mRecorder?.stop()
        mRecorder=null

        stopForegroundService()
        play_Alarm_Sound(this, ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD )
    }

}

1 个答案:

答案 0 :(得分:0)

您需要在.setSound(null)上致电Notification,在.setSound(null, null)上致电NotificationChannel

val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("A service is running in the background")
            .setSound(null)
            .setContentText("Generating random number").build()
val notificationChannel = NotificationChannel(CHANNEL_ID,
            context.getString("My Channel"),
            NotificationManager.IMPORTANCE_DEFAULT)
notificationChannel.setSound(null, null)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificationManager?.createNotificationChannel(notificationChannel)

相关问题