Play phone ringer sound?

rfresh

Well-Known Member
Licensed User
Longtime User
I need to force the ringer sound to play. I'm using a seekbar to set the phone volume so I'd like to play the ringer sound so I can hear the actual volume as I slide the seekbar pad left and right.

Thanks...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub Process_Globals
   Dim mp As MediaPlayer
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      mp.Initialize
   End If
   PlayRinger
End Sub

Sub PlayRinger
   Dim r As Reflector
   r.Target = "ContentDir"
   r.Target = r.RunMethod("intern")   
   mp.Load(r.Target, "content://settings/system/ringtone")
   mp.Play
End Sub
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Sorry for off topic. How to set an audio file as ringtone?
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
I get an error

Mar 23 2012
10:50 Hours

I get an error - java.ioFileNotFoundException using the emulator level 8 BUT OK on my device LG -P500h - Android version 2.2.1.

B4X:
Sub ButtonRing_Click
    Dim r As Reflector  
   Try
      r.Target = "ContentDir"    
      r.Target = r.RunMethod("intern")        
      mp.Load(r.Target, "content://settings/system/ringtone")    
      mp.Play
   Catch
      Msgbox( LastException.Message ,"Ring")
   End Try
End Sub

rtesluk :sign0104:
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I'm trying to make the ringtone change volume when I use a seekbar to adjust the volume. The p.SetVolume line below doesn't change the volume at all as I slide the seekbar to the left.

(In my Main module I can change the volume using this line when I touch the button High it changes the volume to a high value, when I touch the Low button it changes it back down to a low value just fine. But doesn't change the volume in this Setup module.)

B4X:
'Setup module
Sub Globals
   Dim p As Phone
End Sub

Sub seekbar_High_ValueChanged (Value As Int, UserChanged As Boolean)
   If UserChanged = True Then
      Play_Ringer
   End If

   If UserChanged = True Then
      p.SetVolume (p.VOLUME_RING, Value, False)'<---not changing volume
   End If
End Sub

Sub Play_Ringer
    Dim r As Reflector
    r.Target = "ContentDir"
    r.Target = r.RunMethod("intern")    
    Main.mp.Load(r.Target, "content://settings/system/ringtone")
    Main.mp.Play
End Sub
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Mar 23 2012
I get an error - java.ioFileNotFoundException using the emulator level 8 BUT OK on my device LG -P500h - Android version 2.2.1.

You can't use this code on emulator :D
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Cannot change the ringtone volume

I'm attaching a small test app that shows I cannot change the phone ringer volume. The volume is being set via SetVolume and I can see it's being set correctly...but PlayRinger won't play the new values.

I click on Set Volume 4 or 8 or 12 but it plays the same ringtone volume. I must be missing something!!

Also, vibrate won't work either.

Thanks...
 

Attachments

  • phone_test.zip
    6.9 KB · Views: 454
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Just tested it on my Galaxy s2

When I click on Vibrate the phone vibrates.

And yes clicking on any of the other buttons doesn't work either.

I don't know anything about the settings in your code so I can't help locate the problem sorry.

regards, Ricky
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Are you saying when you pressed button Set Volume Level 4 and then pressed the Play Ringtone button there was no sound?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
So here is my simple code to set the ringtone to a volume of 4 with a button press, or a volume of 12 with another button. Then I click the Play button. The ringertone plays but always at the same volume level.

So for some reason, the p.SetVolume() method isn't working.

Is there something else that I need to do to get the volume to play at different levels when I press a volume button in my app?

My label status text verifies that the settings are being made correctly, yet it always plays at the same volume level.

Thanks for any help...

B4X:
Sub Process_Globals
    Dim mp As MediaPlayer
    Dim p As Phone
End Sub

Sub btn_Set_Volume12_Click
   p.SetRingerMode(p.RINGER_NORMAL)
   p.SetVolume (p.VOLUME_RING, 8, True)
   mPhoneVolume = p.GetVolume(p.VOLUME_RING)
   mPhoneMaxVolume = p.GetMaxVolume (p.VOLUME_RING)
   mPhoneRingerMode = p.GetRingerMode
   lbl_Status.Text = "Ringer Mode:" & mPhoneRingerMode & " Volume:" & mPhoneVolume & " Max Volume:" & mPhoneMaxVolume
End Sub

Sub btn_Set_Volume4_Click
   p.SetRingerMode(p.RINGER_NORMAL)
   p.SetVolume (p.VOLUME_RING, 4, True)
   mPhoneVolume = p.GetVolume(p.VOLUME_RING)
   mPhoneMaxVolume = p.GetMaxVolume (p.VOLUME_RING)
   mPhoneRingerMode = p.GetRingerMode
   lbl_Status.Text = "Ringer Mode:" & mPhoneRingerMode & " Volume:" & mPhoneVolume & " Max Volume:" & mPhoneMaxVolume
End Sub

Sub btn_Play_Ringtone_Click
   PlayRinger
End Sub

Sub PlayRinger
    Dim r As Reflector
    r.Target = "ContentDir"
    r.Target = r.RunMethod("intern")    
    mp.Load(r.Target, "content://settings/system/ringtone")
    mp.Play
End Sub
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Other buttons worked but volume never changed to lower or no sound click button.

Volume was 4 when it's started. Click 12 it goes full but never changes back.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I think I've discovered why my code doesn't change the ringer volume. I've been thinking about this problem for days. Erel gave me the code that plays the phone ringer tone via the Media Player. My code below is trying to change the ringer volume but I think it needs to change the media player volume, not the ringer volume.

What I'm trying to do with playing the ringer tone via the media player is to simulate the phone ringing sound and then I'm trying to dynamically adjust the volume...but since I'm using the media player (per Erel suggestion), I need to adjust the media player sound level to simulate adjusting the ringer volume. I'll have to figure out a ratio between the max vol levels of the media player and the ringer max vol.

I've been looking at the Android SDK Media Player docs and see that I will need to use:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);

But I see in b4a popup help for the media player that there is no setAudioStream, no getStreamMaxVolume, no getStreamVolume and no setStreamVolume methods

So can someone help me convert over these 3 SDK methods into b4a methods that I can then use?

Thank you very much...

B4X:
Sub Process_Globals
    Dim mp As MediaPlayer
    Dim p As Phone
End Sub

Sub btn_Set_Volume12_Click
    p.SetRingerMode(p.RINGER_NORMAL)
    p.SetVolume (p.VOLUME_RING, 8, True)
    mPhoneVolume = p.GetVolume(p.VOLUME_RING)
    mPhoneMaxVolume = p.GetMaxVolume (p.VOLUME_RING)
    mPhoneRingerMode = p.GetRingerMode
    lbl_Status.Text = "Ringer Mode:" & mPhoneRingerMode & " Volume:" & mPhoneVolume & " Max Volume:" & mPhoneMaxVolume
End Sub

Sub btn_Set_Volume4_Click
    p.SetRingerMode(p.RINGER_NORMAL)
    p.SetVolume (p.VOLUME_RING, 4, True)
    mPhoneVolume = p.GetVolume(p.VOLUME_RING)
    mPhoneMaxVolume = p.GetMaxVolume (p.VOLUME_RING)
    mPhoneRingerMode = p.GetRingerMode
    lbl_Status.Text = "Ringer Mode:" & mPhoneRingerMode & " Volume:" & mPhoneVolume & " Max Volume:" & mPhoneMaxVolume
End Sub

Sub btn_Play_Ringtone_Click
    PlayRinger
End Sub

Sub PlayRinger
    Dim r As Reflector
    r.Target = "ContentDir"
    r.Target = r.RunMethod("intern")    
    mp.Load(r.Target, "content://settings/system/ringtone")
    mp.Play
End Sub
 
Last edited:
Upvote 0
Top