android.media.Ringtone.play() stops working after a 28 plays - android

I have an app that is open for hours and uses a background service with foreground notification attached to it. Every once in a while a sound is played using:
try {
Ringtone r = RingtoneManager.getRingtone(context, uri);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
The sounds are working, but after a while the sounds don't play anymore.
No errors, no warnings, no crashes. Just no sound.
My users are complaining as well, so it doesn't look like a device specific issue.
Android Docs don't mention anything about this.
Anyone knows why this is?

Don't create a Ringtone or MediaPlayer in a BroadcastReceiver!
The problem was the context of the Ringtone or MediaPlayer. By triggering the sound form a broadcast receiver a new MediaPlayer or Ringtone was created every time the sound played. After 28 times, the sound just didn't play anymore.
By playing the sound from an IntentSerice, or re-useing a static instance of the MediaPlayer or Ringtone, everything played as expected.

Related

I am looking for a way to play an audio message to recipient of a phone call as soon as he/she receives the call [duplicate]

This question already has answers here:
Inject audio into voice stream in android
(2 answers)
Closed 3 years ago.
I am looking for a way to play an audio message to recipient of a phone call as soon as he/she receives the call. I want to push my audio when someone receives the call and then I start conversation. Example:- When some organization calls they start with a greetings audio message first then start the actual conversation.
I have tried searching a lot but all the questions related to this on Stackoverflow are bit old and they mention that this is not possible. Is it still the case?
Could someone please guide how one could achieve this in android?
You can play and control the audio files in android with the help of the MediaPlayer class.
MediaPlayer: This class is the primary API for playing sound and video.
AudioManager: This class manages audio sources and audio output
Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio. How to play audio in android..
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}

How to play the default caller tune sound....?

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).

Android alarm sound doesn't work first time on real device

I have developed an android app which includes an AlarmReceiver with an optional reminder sound (0.5sec mp3 resource via MediaPlayer).
The AlarmReceiver works fine. But the reminder sound exhibits erratic behavior while playing on real devices.
At first attempt on a real device it fails to work, but starts to work fine after it.
I tried uninstalling the application and deleting its data but still result is same.
Any ideas appreciated.`
if (checkbox1_status == 1)
{
MediaPlayer mp = MediaPlayer.create(arg0, R.raw.bierflasche);
mp.start();
}

android how to know ringtone length

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.

Receiving MP3 play actions in a BroadcastReceiver in Android

I am trying to build an Android Service that should get notified when the user starts playing an MP3. I checked LogCat when I start playing a song and saw that the following Intent is logged:
Intent { act=com.android.music.PLAYBACK_VIEWER flg=0x4000000 cmp=com.android.music/.MediaPlaybackActivity }
I couldn't figure out how to write an IntentFilter to let my Service know that this event has occurred and let me know the name of the song that will be played. I searched Android reference but could not find anything on PLAYBACK_VIEWER.
Thanks,
C
I would do neither. First, none of this is part of the SDK and so may change at any point. Second, this will only work for the built-in media player application, not any third-party or OEM-supplied media players, and I expect more people to gravitate to those.

Categories

Resources