Search code examples
androidandroid-mediaplayerandroid-alarms

Playing voicemail via MediaPlayer turns alarm volume silent


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


Solution

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