I am making a calling app and while the user is waiting for the call to connect, I want to play the default sound which is played when we call someone and wait for the correspondent to pick up.
You have to implement RingtoneManager for play default ringtone of system, have look
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone ringtone = RingtoneManager.getRingtone(this,uri);
ringtone.play();
Hope it will help you!!
It is not possible as such.
You are talking about the ringing tone, also called ringback tone sometimes. It is not generated by the Android system, but the switching system, so you don't have access to it from the API.
To include that sound in your app, you have to include an asset for that sound (mp3).
Related
I'd like to develop an application that will play the ringtone at its maximum volume, no matter the user's setting (just how Find My Device works).
This is my code snippet:
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone ringtoneManager = RingtoneManager.getRingtone(this, ringtone);
ringtoneManager.play();
But couldn't be able to find anything regarding the case I mentioned above. Thank you.
I'm working on a VoIP app and when a call comes in, the ringtone is not audible on at least a Samsung A20e and a Samsung A71 device running on Android 11. Unfortunately, this info came from a few users who say they experience the issue and the issue doesn't arise on all the phones I have access to, so I'm not able to look in the logs myself.
To get the ringtone URI, the following code is used:
val uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE)
Then that URI is used on the notification channel:
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
notificationChannel.setSound(uri, attributes)
And the URI is used once again in the method setSound of the NotificationCompat.Builder to generate the actual notification.
I tried to reproduce the issue on an emulator and changed the values in setUsage and setContentType a lot, but nothing seemed to have impact. I also reinstalled the app on every change, to make sure a new notification channel would be created.
Then I read somewhere on the internet that ringtones never work on an emulator. However, I do have an emulator on which the ringtone is actually audible, so I'm not so sure that's true, at least not for every configuration.
Lastly I should add that the ringtone also wasn't silent on my original emulator when I added the following line:
RingtoneManager.getRingtone(context, uri).play()
However, I'm not actually able to use that piece of code, since I need to attach the ringtone to a notification (channel).
Does anyone have experience with a same type of issue? Is there a problem with the settings of my notification or the notification channel? Is this just a Samsung issue? I hope someone can help me out.
I'll answer my own question. Turns out the client didn't describe the issue thorough enough and it turned out to be an easily solvable issue when he provided us with more information. The ringtone would be audible when the notification volume of the phone was on and the ringtone volume of the phone was off. However, when the notification volume was off and the ringtone volume was on, the ringtone wouldn't be audible. So the two of those got switched around, which got fixed by replacing AudioAttributes.USAGE_NOTIFICATION with AudioAttributes.USAGE_NOTIFICATION_RINGTONE.
I have an app that can emit custom notification sounds triggered by calendar events. This worked perfectly well on my old phone, but on my new Galaxy S21, it emits the system default notification sound instead of the custom notification sound. I tried it on the emulator with an AVD running Android 11, which is the version that the S21 claims to be running, and it works correctly. The custom sound exists on the S21 and is playable using the music player.
Android Notification sound defaulting back instead of playing custom sound is a very old question reporting a similar problem and the answer there suggested rebooting the phone, which I tried and it didn't help.
The code which emits the notification looks like this:-
private void emitNotification(String smallText, String bigText, String path) {
RemoteViews layout = new RemoteViews(
"uk.co.yahoo.p1rpp.calendartrigger",
R.layout.notification);
layout.setTextViewText(R.id.notificationtext, bigText);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
layout.setTextViewText(R.id.notificationtime,
df.format(System.currentTimeMillis()));
Notification.Builder NBuilder
= new Notification.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(smallText)
.setContent(layout);
if ((path != null) && !path.isEmpty())
{
Uri uri = new Uri.Builder().path(path).build();
AudioAttributes.Builder ABuilder
= new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
NBuilder.setSound(uri, ABuilder.build());
}
// Show notification
NotificationManager notifManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(notifyId++, NBuilder.build());
}
The entire source is available on GitHub at
rparkins999/CalendarTrigger branch fix3.3
I have included a built APK in the git tree if anyone would like to try it on another phone. You can also build from the sources: you will need a ../Keys/Keystore.properties file relative to the project root directory since I'm not publishing my private signing key.
To demonstrate a custom notification sound, run the program, give it all the permissions it wants, touch 'NEW EVENT CLASS', give the class a name, and touch Create: you should then see a screen consisting of buttons that invoke activities to define the characteristics of the class.
Touch 'Event starts action(s) for class ...' and you should see a screen offering various actions that the program can take (not all of which work). Touch 'Show a notification', then 'Play a sound', then 'Browse for a sound file': you should then see a file browser.
When you choose a file it will take you back to the previous screen, but 'Browse for a sound file' will have been replaced by the path to the sound file. Touch the back button to return to the screen of buttons, and then touch 'Immediate event of class ...'. It should play the sound.
On the emulator it does, but on the S21 it plays the default sound instead.
As set up, the class you created will emit a notification on every calendar event. You can stop that by deleting the class or setting some conditions for a calendar event to be in the class: there are buttons for both of these actions on the class definition screen.
I don't know whether this problem is a bug in the S21, or caused by some obscure Setting that I haven't been able to find. Any help would be appreciated, especially information as to whether the app does or doesn't work correctly on other phones running Android 11. Of course, a fix or a workaround would be better still!
I just installed the latest S21 update. Now the problem is worse! I don't get any sound at all from my notifications with custom sounds.
The problem in this particular case was the audio format in the file (.m4a). The music player on the S21 can play it, and the notification sound player in my old phone could play it, but the notification player on the S21 apparently couldn't play it.
I used FFmpeg on my PC to convert from .m4a to .mp3, which the S21 notification player can play, and it now works.
So it isn't safe to assume that if the phone can play a sound format with its music player, the Notification logic can also handle it.
I'm working on a sample code same as http://developer.android.com/guide/topics/media, I'm successfully playing the ringtone, but I want to know the length of the ringtone(in sec) or atleast a notification after end of the ringtone, so that i have to do some other task after playing the ringtone.
Thanks
nehatha
Put it in a MediaPlayer. You dont have to play it just .prepare() it. Then you can ask MediaPlayer about the duration.
I want to know how to get the ringtone,audio files in the device and I want set them as alarm. How to do it?
I am working in the code to set the alarm and I want to know how to retreive audio or ringtones from the device.
Read the docs on RingtoneManager and Ringtone
RingtoneManager
Ringtone