android studioでkotlinにてアラーム機能を実装しようとしております。
〇やりたいこと
設定時間に通知(Notification)と音(SoundPool)を出したい
以下コードは5s後に通知されるように書いています
〇困っていること
指定時間にSoundPoolの処理を通過しているものの音が鳴らない
MainActiviy
1class MainActivity : FragmentActivity(), TimePickerDialog.OnTimeSetListener { 2 3 private lateinit var binding: ActivityMainBinding 4 private lateinit var soundManager: SoundManager 5 6 override fun onCreate(savedInstanceState: Bundle?) { 7 super.onCreate(savedInstanceState) 8 binding = ActivityMainBinding.inflate(layoutInflater) 9 setContentView(binding.root) 10 11 soundManager = SoundManager(this) 12 13 binding.btnTest.setOnClickListener { 14 alarmSet() 15 } 16 } 17 18 private fun alarmSet() { 19 val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager 20 val calendar = Calendar.getInstance() 21 22 var year = calendar.get(Calendar.YEAR) 23 var month = calendar.get(Calendar.MONTH) 24 var day = calendar.get(Calendar.DAY_OF_MONTH) 25 26 val intent = Intent(this, MyReceiver::class.java) 27 val pendingIntent = PendingIntent.getBroadcast( 28 this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT 29 ) 30 31 calendar.add(Calendar.SECOND, 5) 32 alarmManager.setExact( 33 AlarmManager.RTC_WAKEUP, 34 //calendar.getTimeInMillis(), 35 calendar.timeInMillis, 36 pendingIntent 37 ) 38 } 39 40 fun playOneReady() { 41 soundManager.playOne() 42 }
MyReceiver
1class MyReceiver: BroadcastReceiver() { 2 override fun onReceive(context: Context?, intent: Intent?) { 3 if (context != null) { 4 MyNotification.sendNotification(context) 5 } 6 } 7}
MyNotification
1class MyNotification(context: Context) { 2 3 companion object { 4 private const val NOTIFICATION_CHANNEL_ID = "com.example.sample_alarm_manager_channel_id" 5 private const val NOTIFICATION_CHANNEL_NAME = "Time for !!" 6 private const val NOTIFICATION_CHANNEL_DESCRIPTION = "Go To The" 7 private const val NOTIFICATION_TITLE = "sample alarm manager" 8 private const val NOTIFICATION_MESSAGE = "test message." 9 10 fun sendNotification(context: Context) { 11 Log.d("debug", "MyNotification sendNotification") 12 13 val channelId = Companion.NOTIFICATION_CHANNEL_ID 14 val channelName = Companion.NOTIFICATION_CHANNEL_NAME 15 val channelDescription = Companion.NOTIFICATION_CHANNEL_DESCRIPTION 16 17 //Android 8.0 以上ではアプリの通知チャンネルを登録することが必要。 18 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 19 val importance = NotificationManager.IMPORTANCE_DEFAULT 20 val channel = NotificationChannel(channelId, channelName, importance).apply { 21 description = channelDescription 22 } 23 val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 24 manager.createNotificationChannel(channel) 25 } 26 27 //通知をシステムに登録しています。 28 val builder = NotificationCompat.Builder(context, channelId).apply { 29 setSmallIcon(R.drawable.ic_baseline_info_24) 30 setContentTitle(Companion.NOTIFICATION_TITLE) 31 setContentText(Companion.NOTIFICATION_MESSAGE) 32 priority = NotificationCompat.PRIORITY_DEFAULT 33 } 34 35 val id = 0 36 NotificationManagerCompat.from(context).notify(id, builder.build()) 37 38 val mainActivity = MainActivity() 39 mainActivity.playOneReady() 40 } 41 } 42 43}
soundManager
1class SoundManager(context: Context) { 2 3 private var soundPool: SoundPool 4 private var soundOne = 0 5 private var streamId2 = 0 6 7 init { 8 //soundPool設定 9 val audioAttributes = AudioAttributes.Builder() 10 .setUsage(AudioAttributes.USAGE_GAME) 11 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) 12 .build() 13 14 soundPool = SoundPool.Builder() 15 .setAudioAttributes(audioAttributes) 16 .setMaxStreams(1) 17 .build() 18 19 soundOne = soundPool.load(context, R.raw.bivlla, 1) 20 Log.d("debug", "SoundManager init") 21 } 22 23 fun playOne() { 24 cancelOne() 25 streamId2 = soundPool.play(soundOne, 1.0f, 1.0f, 0, -1, 1.0f) 26 Log.d("debug", "sound ringing") 27 } 28 29 fun cancelOne() { 30 if(streamId2 != 0) { //fun playOneを通るとここを通る(音が鳴るとstreamId2= 1 が入る) 31 soundPool.stop(streamId2) 32 streamId2 = 0 33 } 34 } 35}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。