![](/img/trans.png)
[英]Want to upload audio to sound cloud via my server, how can i check to upload songs only from External storage directory
[英]Problem with changing sound alarm in Kotlin when I already reselect audio from external storage
我想嘗試在 Kotlin 中進行鬧鍾 Android 應用程序實驗,以便它可以更改來自外部存儲的音頻聲音,假設我已經為來自外部存儲的鬧鍾聲音設置了“A”音頻聲音。 然后,我想通過重新選擇它來將設置為“B”音頻聲音的音頻輸入聲音更改為來自外部存儲器的警報聲音。 問題是,當我已經通過重新選擇將警報聲音的音頻輸入從音頻“A”更改為音頻“B”時,音頻“A”(上一個音頻)仍在播放警報聲音而不是音頻“B”。
所以這是 select 音頻按鈕:(忽略 requestPermission(),假設已授予權限)
binding.selectAudio.setOnClickListener()
{
if (checkPermission())
{
openAudio()
}
else
{
requestPermission()
}
}
這是當應用程序重定向到選擇音頻聲音並獲取其路徑時:
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()
}
}
}
這是設置鬧鍾按鈕:
binding.setAlarmBtn.setOnClickListener()
{
createNotificationChannel(soundPath)
setAlarm()
Log.e("notif", "soundpath" + soundPath.toString())
Log.d("soundPath", soundPath.toString())
}
這是設置鬧鍾到一定時間的過程:
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()
}
}
最后,這是通過通知播放警報以及在輸入音頻聲音后獲取音頻 uri 路徑的時候:
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)
}
}
此外,這是來自 BroadcastReceiver 的 onReceive 聲音:
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())
}
}
那么,當鬧鍾正在播放新的輸入聲音而不是以前的輸入聲音時,如何解決這個問題以使鬧鍾聲音發生變化?
Android 不允許通過重新創建來更新通知頻道的聲音。 您應該先刪除現有頻道,然后再創建新頻道。 所以,試試看:
private fun createNotificationChannel(path: String?)
{
...
...
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.deleteNotificationChannel("alarmManagerDemo")
notificationManager.createNotificationChannel(channel)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.