Android Question How to play sounds in ALARM audio channel

aregan

Member
Licensed User
Hi

I'm trying to play an audio alarm when the phone is locked to alert the user.

I have been using the SoundPool.Play method, but this plays by via the Music / media channel which the user typically has turned down on their device.
I've adjusted the Play volume in the PLAY call, but the volume is a still prodeminatly controlled by the users media volumn setting.

So now I'm looking to play the alert via the ALARM channel as I'm hoping that the volume on that will not be adjusted by the user as often.

I notice on the android documentation that the SoundPool initializer can take a few paramenters. One of them is the channel to play on - such as the ALARM channel.
In B4A it looks like we can only initialize the soundpool on the default Music / Media channel.

Do you know if it is possible to play audio filles via the ALARM channel ? Is there a way to set the soundpool to use the prefered channel ?

Thanks
Alan
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

aregan

Member
Licensed User
Thanks Erel

The code above worked perfectly.
For anyone else - you just need to declare an instance of the phone object to use the Phone.Volume_alarm .... Dim phone as Phone
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
Thanks Erel

The code above worked perfectly.
For anyone else - you just need to declare an instance of the phone object to use the Phone.Volume_alarm .... Dim phone as Phone
@aregan, please, before you go! How did you play it without media player ? Below code is not working for me
Dim phone As Phone Dim sp As SoundPool Dim jo As JavaObject jo.InitializeNewInstance("android.media.soundpool", Array(4, phone.VOLUME_ALARM, 0)) sp = jo Dim PoolID As Int = sp.Load(File.DirInternal, name) sp.Play(PoolID,1,1,1,0,1)
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
The Android namespaces are case-sensitive. Try changing
B4X:
jo.InitializeNewInstance("android.media.soundpool", Array(4, phone.VOLUME_ALARM, 0))
to
B4X:
jo.InitializeNewInstance("android.media.SoundPool", Array(4, Phone.VOLUME_ALARM, 0))
(note your namespace versus the one @Erel specified).
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
The Android namespaces are case-sensitive. Try changing
B4X:
jo.InitializeNewInstance("android.media.soundpool", Array(4, phone.VOLUME_ALARM, 0))
to
B4X:
jo.InitializeNewInstance("android.media.SoundPool", Array(4, Phone.VOLUME_ALARM, 0))
(note your namespace versus the one @Erel specified).
Thanks, Jeffrey, now it doesn't crash, but I couldn't here any sound play on .wav files. Is anything still needed in the code? It crashed before but stable now, but no sound play
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
The Android namespaces are case-sensitive. Try changing
B4X:
jo.InitializeNewInstance("android.media.soundpool", Array(4, phone.VOLUME_ALARM, 0))
to
B4X:
jo.InitializeNewInstance("android.media.SoundPool", Array(4, Phone.VOLUME_ALARM, 0))
(note your namespace versus the one @Erel specified).
I tuned alarm volume to the highest from phone setting, no sound yet
 
Upvote 0

aregan

Member
Licensed User
@aregan, please, before you go! How did you play it without media player ? Below code is not working for me
Dim phone As Phone Dim sp As SoundPool Dim jo As JavaObject jo.InitializeNewInstance("android.media.soundpool", Array(4, phone.VOLUME_ALARM, 0)) sp = jo Dim PoolID As Int = sp.Load(File.DirInternal, name) sp.Play(PoolID,1,1,1,0,1)


Hi Omo

This is the code that works for me:

B4X:
    Dim jo As JavaObject
    Dim ph As Phone
    Dim AlertSoundPlayID As Int

    jo.InitializeNewInstance("android.media.SoundPool", Array(4, ph.VOLUME_ALARM, 0)) '4 = max streams
    soundsAlarmChannel = jo
    soundid_notification = soundsAlarmChannel.Load(File.DirAssets,"notification.wav")
    AlertSoundPlayID = soundsAlarmChannel.Play(soundid_notification,1,1,1,-1,1)
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
Hi Omo

This is the code that works for me:

B4X:
    Dim jo As JavaObject
    Dim ph As Phone
    Dim AlertSoundPlayID As Int

    jo.InitializeNewInstance("android.media.SoundPool", Array(4, ph.VOLUME_ALARM, 0)) '4 = max streams
    soundsAlarmChannel = jo
    soundid_notification = soundsAlarmChannel.Load(File.DirAssets,"notification.wav")
    AlertSoundPlayID = soundsAlarmChannel.Play(soundid_notification,1,1,1,-1,1)
@aregan, thank you. I later noticed you've gone offline even before I said don't go?
Thanks, enjoy your day!
 
Upvote 0
Top