I want to try to make alarm Android app experiment in Kotlin so it can change audio sound from external storage, say that I already set the "A" audio sound for alarm sound from the external storage. Then, I want to change audio input sound that set into "B" audio sound for alarm sound from the external storage by reselecting it. The problem is, when I already change the audio input for alarm sound from audio "A" to audio "B" by reselecting it, audio "A" (previous audio) still playing the alarm sound instead of audio "B".
So this is the select audio button : (ignore the requestPermission(), supposse the permission is granted)
binding.selectAudio.setOnClickListener()
{
if (checkPermission())
{
openAudio()
}
else
{
requestPermission()
}
}
This is when the app redirected into selecting audio sound and get the path on it:
private fun openAudio()
{
//soundPath = null
onStop()
val openFileIntent = Intent(Intent.ACTION_GET_CONTENT)
openFileIntent.type = "audio/*"
getResult.launch(openFileIntent)
}
private val getResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{
soundPath = null
if (it.resultCode == RESULT_OK)
{
val uri: Uri? = it.data!!.data
File(uri!!.getPath())
val path = getRealPath(this, uri)
soundPath = path
if (soundPath != null)
{
Toast.makeText(this, "Audio file has been set", Toast.LENGTH_SHORT).show()
}
}
}
This is the set alarm button :
binding.setAlarmBtn.setOnClickListener()
{
createNotificationChannel(soundPath)
setAlarm()
Log.e("notif", "soundpath" + soundPath.toString())
Log.d("soundPath", soundPath.toString())
}
This is the process of setting alarm into certain time :
private fun setAlarm()
{
try
{
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
alarmManager[AlarmManager.RTC_WAKEUP, calendar.timeInMillis] = pendingIntent
Toast.makeText(this, "Alarm set successfully", Toast.LENGTH_SHORT).show()
}
catch (e: Exception)
{
Toast.makeText(this, "Time is not been set", Toast.LENGTH_SHORT).show()
}
}
And finally this is when alarm is playing through notification as well as get audio uri path after input audio sound :
private fun createNotificationChannel(path: String?)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val uri = getUri(path)
val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build()
val name : CharSequence = "AlarmReminderChannel"
val description = "Channel For Alarm Manager"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel("alarmManagerDemo", name, importance)
channel.description = description
channel.setSound(uri, attributes)
Log.e("notif", "uri" + uri.toString())
Log.e("notif", "uri from channel" + channel.sound.toString())
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
private fun getUri(path: String?) : Uri?
{
Log.e("notif", "path: "+path )
if(path == null)
{
Log.e("notif", "null" )
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.packageName + "/raw/music")
}
else
{
Log.e("notif", "tidak null" )
return Uri.parse(path)
}
}
Also, this is the onReceive sound from BroadcastReceiver :
class AlarmReceiver : BroadcastReceiver()
{
override fun onReceive(context: Context?, intent: Intent?)
{
val bundle : Bundle? = intent!!.extras
val i = Intent(context, DestinationActivity::class.java)
intent!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_IMMUTABLE)
val builder = NotificationCompat.Builder(context!!, "alarmManagerDemo")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Demo")
.setContentText("Bla bla bla bla bla bla bla bla")
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(123, builder.build())
}
}
So, how do I solve this to make the alarm sound changed when the alarm is playing new input sound instead of previous input sound?
Android doesn't permit updating the sound of the notification channel by recreating it. You should first delete exist channel, and after create the new. So, try it: