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

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();
}

Related

How to prevent Android from automatically vibrating on MediaPlayer playback

I have an alarm clock app and some users complain phone vibrates during alarm, while vibrations should be disabled.
After some digging I have found out, that on some devices there's a system option for device to vibrate along alarm music. For example in my test Pixel 4 it is located at Settings->Sound&Vibration->Vibration&Heptics->Alarm vibration.
What this setting, enabled by default, causes, is that vibrations try to "emulate" the music played through MediaPlayer and I cannot find a way to prevent that from happening from within the app or even detect if such setting is present/enabled.
Anyone knows how to get rid of that?
Here's a sample method I used for testing:
private fun startThePlayer(context: Context, playInLoop: Boolean = true) {
try {
mediaPlayer.reset()
mediaPlayer.isLooping = playInLoop
val uri = Settings.System.DEFAULT_RINGTONE_URI
mediaPlayer.setDataSource(context, uri)
mediaPlayer.setOnPreparedListener {
mediaPlayer.start()
}
mediaPlayer.prepareAsync()
} catch (e: Exception) {
log(e.toString())
}
}
VIBRATE permission is necessary for this to work.
Effect on Pixel 4 with Android 13:
Device is vibrating, as if trying to "emulate" the music played. Vibrations strength depends on value set in device's settings, completely ignoring volume set for alarm's music, and also messing up any vibrations set directly in my app.
What's interesting, is that for some reason Android's default clock app ignores this settings and device doesn't vibrate during it's alarms.
First, I suggest that you use android's VibratorManager to control your app vibrations settings (this is how the android development team called this service.. how bizarre). Use the getDefaultVibrator and cancel methods to stop any vibrations produced by your app (official docs to the rescue)
Second, because your app is an alarm clock please consider using the setWakeMode which will allow you to keep your app running in the background regardless to the display (in order to prevent the alarm from stopping if the screen is off). Here's the method documentation and also the PowerManager flags documentation
Hope you will find the right combination to satisfy your needs

Ringtone not playing on certain Samsung devices with Android 11

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.

Why is Galaxy S21 not doing custom notification sounds

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.

MediaStore.Audio.Media.RECORD_SOUND_ACTION not working in nougat

I am using MediaStore.Audio.Media.RECORD_SOUND_ACTION to open sound recorder application, I am not able to open application as no default application present, then i install two voice recorder application even though not able to see these application in my chooser intent. I am using following code-
Intent soundRecorderIntent = new Intent(); // create intent
soundRecorderIntent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION); // set action
startActivityForResult(soundRecorderIntent, ACTIVITY_RECORD_SOUND); // start activity
It works well in marshmallow
The code is correct and you've probably found an app that supports the intent by now. But to prevent that others waste their time like I did, here's a quick note:
Most of the currently top rated voice recording apps in the Play Store do not provide the necessary intent filter.
That seemed so unrealistic to me that I doubted my code when it didn't work after having installed the five most popular voice recording apps. But there's a handy manifest viewer app that reveals that their manifests simply do not declare the intent filter. So, as said, the code is correct.
To save you the time from searching for a suitable app, here are two that are invocable:
Audio Recorder from Sony
Samsung Voice Recorder from Samsung
There is no requirement for a voice recorder app to support this Intent action, and apparently your device does not ship with an app that supports this Intent action either. Your code itself seems fine.
If recording is optional to your app, catch the ActivityNotFoundException and tell the user that they do not have a recorder, and perhaps suggest to them that they install one that you test that works, if you can find one.
Otherwise, record the audio yourself, using MediaRecorder.

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