Android Question Default message sound

Discussion in 'Android Questions' started by francoisg, Jul 22, 2015.

  1. francoisg

    francoisg Active Member Licensed 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?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Use this code instead (it will work on all versions):
    Code:
    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.
     
    Jmu5667 and luke2012 like this.
  3. imbault

    imbault Well-Known Member Licensed 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 :
    Code:
    mp.Load(File.DirAssets,cSoundFile)     'V1.09
    mp.SetVolume(11)
    mp.Play
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This method is only relevant for ringtones.

    You can use MediaPlayer to play files from the assets folder.
     
    imbault likes this.
  5. francoisg

    francoisg Active Member Licensed User

    Thank you Erel! I will update my code and test ...
     
Loading...