Android Question Default message sound

francoisg

Active Member
Licensed User
Longtime User
Hi,
I am using the following code to play a "message" ringtone.

Sub makeNotificationsound
' ** Vibrate ...
Dim pv As PhoneVibrate
pv.Vibrate(1000)
' ** Play default authentication sound ...
Dim mp As MediaPlayer
mp.Initialize
' ** Use ringtone manager to get default sound ...
Dim rm As RingtoneManager
mp.Load(rm.GetContentDir, rm.GetDefault(rm.TYPE_NOTIFICATION))
mp.Play​
End Sub

After upgrading to Android 5.02, I get the following error: "Direct file access no longer supported; ringtone playback is available through android.media.Ringtone"

What is the proper way to play a default sound?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use this code instead (it will work on all versions):
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim rm As RingtoneManager
   PlayRingtone(rm.GetDefault(rm.TYPE_NOTIFICATION))
End Sub

Sub PlayRingtone(url As String)
   Dim jo As JavaObject
   jo.InitializeStatic("android.media.RingtoneManager")
   Dim jo2 As JavaObject
   jo2.InitializeContext
   Dim u As Uri
   u.Parse(url)
   jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("play", Null)
End Sub
Requires JavaObject and ContentResolver libraries.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
@Erel , thank you, that's interesting, can we play a sound stored in File.DirAssets with this method, because I use the play method in my notification routine
like :
B4X:
mp.Load(File.DirAssets,cSoundFile)     'V1.09
mp.SetVolume(1, 1)
mp.Play
 
Upvote 0
Top