3

I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?

Code for the file I am using to control the voicemail playback. http://hastebin.com/ehijobuwoc.coffee

Leo
  • 4,652
  • 6
  • 32
  • 42
  • if it can help, try to add setOnCompletionListener() method of media player and reset the values in this.... – Deepak Goyal Aug 21 '15 at 22:56
  • @DeepakGoyal as you can see I have that, and I don't think it is doing anything. Also potentially the voicemail may not be played all the way. – Leo Aug 21 '15 at 23:03
  • You are using `setSpeakerphoneOn(false)`, you should probably set it back to true as soon as stopping the playback. – ozbek Aug 30 '15 at 08:55
  • Thought that might be it @ozbek but unfortunately that didn't do anything. In fact just calling that, even without playing the voicemail broke it. – Leo Aug 31 '15 at 23:20

2 Answers2

0

You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.

http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)

DreadfulWeather
  • 716
  • 3
  • 13
  • Nope, didn't do anything. – Leo Sep 03 '15 at 00:09
  • May i ask why do you call setMode and setSpeakerPhoneOn methods ? And can you also try using getRingerMode at start then using setRingerMode at finish with returned value from getRingerMode ? – DreadfulWeather Sep 03 '15 at 00:46
  • should I not be using setMode/setSpeakerPhoneOn? Ok so I figured at least something out, the way I have the code right now, lets me play the media through earpiece instead of the speakerphone. If I comment out setMode(it plays through speaker and the alarm doesn't break)... But I need both. – Leo Sep 03 '15 at 01:18
  • In documentation it says setMode/setSpeakerPhoneOn should only be used by applications that replace the platform-wide management of audio settings or the main telephony application. I think you should not use both of them and it will automatically play on earpiece if earpiece plugged in or speaker if it is not. – DreadfulWeather Sep 03 '15 at 01:24
  • Not quite a solution, but yes I think it may be the issue. The requirement is to play the voicemail not over speaker. – Leo Sep 03 '15 at 01:30
  • Then you can check it like in the link and dont play audio if the route is speaker [link](https://developer.android.com/training/managing-audio/audio-output.html) – DreadfulWeather Sep 03 '15 at 01:32
0

Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.

I needed to setMode back to what it was when I started, in onStop():

// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);

and in init:

private void initMediaPlayer() {
    // Make the media play through earpiece instead of through speaker(default)
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    prevMode = am.getMode();
    am.setMode(AudioManager.MODE_IN_COMMUNICATION);
    am.setSpeakerphoneOn(false);

Thanks @DreadfulWeather for leading me to this answer, with some helpful questions.

Leo
  • 4,652
  • 6
  • 32
  • 42