I have some code that plays a small notification beep from an mp3 (included in res/raw) that works fine on regular Android devices (various 2.2 and up), which I am trying to also use on Google TV and it doesn't seem to do anything, no errors, just no sound.
What could I be doing wrong (this has to be something simple that I am missing?).
Here is the basic code I'm using:
private void beep() {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
try {
Log.d(App.LOG_TAG, "playing the damn beep ****************");
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.beep);
// tried with and without volume
///mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.setLooping(false);
mediaPlayer.start();
} catch (Exception e) {
Log.e("beep", "error: " + e.getMessage(), e);
}
} else {
Log.d(App.LOG_TAG, "could not gain audio focus");
}
}
(NOTE: In real life I don't recreate the MediaPlayer and AudioManager every time, but I tried just putting everything in one method for test purposes. This works fine on the regular Android emulator, and on regular Android [phone] devices, but plays no sound on GTV emulator or Logitech Revue -- even though it does get into the MediaPlayer block fine and logs that it's playing the beep.)
Did you enable Notification sound in Settings->Picture & sound ->Notification sounds->Sounds->Default ?
I had the same problem. For whatever reason, it looks like the files need to be in ogg format. I tried .mp3, and a few versions of .wav. Even the GTV dev docs (https://developers.google.com/tv/android/docs/gtv_media_formats#StandardFormats) say .mp3 is okay.
My HiSense Pulse Google TV only plays the files if they are .ogg files.
This online converter seems to work: http://media.io/
Related
I am trying to build a custom type of alarm clock for Android, and I have set up a settings page to allow the users to choose which alarm sound they would like to use. After they select it, it is placed into SharedPreferences, easy enough.
The problem I am having is how to play the alarm they have chosen, while using the RingtoneManager.TYPE_ALARM, so that the volume is based off the systems alarm sound instead of the systems notification volume.
I have tried multiple ways to even get it to use the Alarm volume, but nothing seems to be doing the trick.
1.
Uri currentRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
currentRingtone = RingtoneManager.getRingtone(context, currentRingtoneUri);
currentRingtone.play();
2.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String syncConnPref = sharedPref.getString("alarm_new_alarm_sound", null);
if (syncConnPref != null) {
RingtoneManager.getRingtone(this, Uri.parse(syncConnPref)).play();
}
I cannot seem to find any other direct ways of playing the actual alarm volume sound. Everything online looks at just playing ringtones.
I would really appreciate if anyone had any insight on using the alarm volume.
Somewhat old thread, but maybe this will help someone in the future.
I am currently working on an alarm clock myself and I'm doing this:
int audioFile = prefs.getInt("audioFile", R.raw.yourFile);
MediaPlayer mp = new MediaPlayer();
try {
mp.reset();
mp.setDataSource(PlayerActivity.this,
Uri.parse("android.resource://your.package.here/" + audioFile));
mp.setAudioStreamType(STREAM_ALARM);
mp.setLooping(true);
mp.prepare();
mp.start();
}
catch (...) {
...
}
It's working fine for me.
There seems to be an issue while trying to play audio files on earpiece in some android devices. Expected behaviour is it should play the audio file in earpiece but in some devices like Samsung galaxy note 2 ( Android version 4.4.2) and Sony Xperia XA ( Android version 6.0) it plays them through speakers.
However in devices like Motorola moto g (android version 6.0) and nexus 5X (android version 7.0) it works fine.
The app also has the android.permission.MODIFY_AUDIO_SETTINGS permission.
Please find the details of this issue below.
Before playing an audio I set the following properties:
audioManager = (AudioManager)configService.getContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(isEnabled);
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
player.setDataSource(path);
I have tried setting mode to MODE_IN_CALL, but that didn't work either:
audioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
I have also tried solutions from other posts asking similar questions but they didn't help much.
It would be helpful to know whether this issue is device specific and how to fix the same.
Thanks for the help.
Finally i found the solution. When registering for intent ACTION_HEADSET_PLUG in BroadcastReceiver, it was broadcasting sticky intent for the headset unplugged event which was saved from the past event. This was causing the callback to fire which played the audio through speaker only.
Refer to the following stack overflow post for further details: link
To fix the issue i used isInitialStickyBroadcast() method to filter out the past events.
public void onReceive(Context context, Intent intent) {
switch (intent.getIntExtra("state", -1)) {
case 0:
if (!isInitialStickyBroadcast()) {
// headset unplugged
}
break;
case 1:
// headset plugged in
break;
default:
break;
}
}
I've been using ACTION_HEADSET_PLUG but had the same problem with playing sound through earpiece on some devices 6.0 +. My solution is to use AudioAttributes for devices with version higher then LOLLIPOP :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes mAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.build();
mMediaPlayer = MediaPlayer.create(this, R.raw.connection, mAttributes, 1);
} else {
mMediaPlayer = MediaPlayer.create(this, R.raw.connection);
}
I'm developing a game. I need a button to turn on and off game sounds.
In my app, I play background music which I want to be muted upon clicking a button. Here's my code:
AudioManager aManager = (AudioManager)getSystemService(AUDIO_SERVICE);
if (aManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
When I debug, the conditions are right but nothing happens! The sound is never turned off!
Check for method "isVolumeFixed()". If true, your device can't have it's sound changed. Some devices have a policy about that.
I have a video without any audio playing in my activity, so if my music player is playing and my app is opened the music stops. I was wondering if there is a way to not request the Audio focus and let the music play in the background even when my video is playing ? I have tried abandoning audio focus after starting the video but that doesn't seem to work either.
Here's the code I have tried till now -
String path = "android.resource://" + getPackageName() + "/" + R.raw.backgroundvideo;
mvvBackground.setVideoURI(Uri.parse(path));
mvvBackground.setMediaController(null);
mvvBackground.start();
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.abandonAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
Log.d(LOG_TAG, "Audio focus changed!");
}
});
Any suggestions will be appreciated ! Thanks
I have not tried this, but according to the docs, for API level 26 and above you can use mVideoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE) to achieve this behaviour.
For below that API level, there is a workaround mentioned here.
I have an app with an integrated music player. I don't want the music to be interrupted by incoming calls.
I use following function for that:
public static void updateDoNotDisturbMode(boolean enabled, boolean checkPrefs)
{
...
AudioManager audioManager = ((AudioManager) MainApp.get().getSystemService(Context.AUDIO_SERVICE));
NotificationManager notificationManager = (NotificationManager) MainApp.get().getSystemService(Context.NOTIFICATION_SERVICE);
// audioManager.setMode(AudioManager.MODE_IN_CALL);
if (!enabled)
{
audioManager.setStreamSolo(AudioManager.STREAM_MUSIC, false);
audioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, false);
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
...
}
else
{
audioManager.setStreamSolo(AudioManager.STREAM_MUSIC, true);
audioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
...
}
}
what works
silent phone if no headphones are connected
internal music player does play on without interruption if someone calls
so without headphones connected, everything works perfectly fine
what does not work
if headphones are connected, the standard beep sound is interrupting my music player and is played in the headphones
How can I avoid that incoming calls are interrupting my music player even if headphones are connected?
TARGET SDK
Only 4.2 and upwarts... (including 5)
What do you mean integrated music player? Can you change code of music app?
A player stops playing because it listens to PhoneStateChanges - void onCallEvent(int state, String number);
If you can change code of the player you should check is mute mode on within this method - if yes then do not stop playback.
If you can't change the code of player - you can use advanced player with preferences of audio focus. For example poweramp has such option. Set Settings/Audio/Audio Focus/Pause in Call to false (it works, tested right now). But you should change it manually all the time.