android studioでkotlinにてアラーム機能を実装しようとしております。
〇やりたいこと
設定時間に通知(Notification)と音(SoundPool)を出したい
以下コードは5s後に通知されるように書いています
〇困っていること
指定時間にSoundPoolの処理を通過しているものの音が鳴らない
MainActiviy
class MainActivity : FragmentActivity(), TimePickerDialog.OnTimeSetListener { private lateinit var binding: ActivityMainBinding private lateinit var soundManager: SoundManager override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) soundManager = SoundManager(this) binding.btnTest.setOnClickListener { alarmSet() } } private fun alarmSet() { val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val calendar = Calendar.getInstance() var year = calendar.get(Calendar.YEAR) var month = calendar.get(Calendar.MONTH) var day = calendar.get(Calendar.DAY_OF_MONTH) val intent = Intent(this, MyReceiver::class.java) val pendingIntent = PendingIntent.getBroadcast( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ) calendar.add(Calendar.SECOND, 5) alarmManager.setExact( AlarmManager.RTC_WAKEUP, //calendar.getTimeInMillis(), calendar.timeInMillis, pendingIntent ) } fun playOneReady() { soundManager.playOne() }
MyReceiver
class MyReceiver: BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (context != null) { MyNotification.sendNotification(context) } } }
MyNotification
class MyNotification(context: Context) { companion object { private const val NOTIFICATION_CHANNEL_ID = "com.example.sample_alarm_manager_channel_id" private const val NOTIFICATION_CHANNEL_NAME = "Time for !!" private const val NOTIFICATION_CHANNEL_DESCRIPTION = "Go To The" private const val NOTIFICATION_TITLE = "sample alarm manager" private const val NOTIFICATION_MESSAGE = "test message." fun sendNotification(context: Context) { Log.d("debug", "MyNotification sendNotification") val channelId = Companion.NOTIFICATION_CHANNEL_ID val channelName = Companion.NOTIFICATION_CHANNEL_NAME val channelDescription = Companion.NOTIFICATION_CHANNEL_DESCRIPTION //Android 8.0 以上ではアプリの通知チャンネルを登録することが必要。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(channelId, channelName, importance).apply { description = channelDescription } val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager manager.createNotificationChannel(channel) } //通知をシステムに登録しています。 val builder = NotificationCompat.Builder(context, channelId).apply { setSmallIcon(R.drawable.ic_baseline_info_24) setContentTitle(Companion.NOTIFICATION_TITLE) setContentText(Companion.NOTIFICATION_MESSAGE) priority = NotificationCompat.PRIORITY_DEFAULT } val id = 0 NotificationManagerCompat.from(context).notify(id, builder.build()) val mainActivity = MainActivity() mainActivity.playOneReady() } } }
soundManager
class SoundManager(context: Context) { private var soundPool: SoundPool private var soundOne = 0 private var streamId2 = 0 init { //soundPool設定 val audioAttributes = AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_GAME) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build() soundPool = SoundPool.Builder() .setAudioAttributes(audioAttributes) .setMaxStreams(1) .build() soundOne = soundPool.load(context, R.raw.bivlla, 1) Log.d("debug", "SoundManager init") } fun playOne() { cancelOne() streamId2 = soundPool.play(soundOne, 1.0f, 1.0f, 0, -1, 1.0f) Log.d("debug", "sound ringing") } fun cancelOne() { if(streamId2 != 0) { //fun playOneを通るとここを通る(音が鳴るとstreamId2= 1 が入る) soundPool.stop(streamId2) streamId2 = 0 } } }
まだ回答がついていません
会員登録して回答してみよう