Java – MediaPlayer volume problem – AudioStream problem

MediaPlayer volume problem – AudioStream problem… here is a solution to the problem.

MediaPlayer volume problem – AudioStream problem

When I get a message in my app, I’m trying to provide a custom beep. This beep should match the primary phone notification volume level (not the ringer volume). This means that if the phone notifies vol = 3/10, the beep intensity should be 3/10.
I can’t do that,

  AudioManager audioMan = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    int volume;

if (mPlayer == null) {
        mPlayer = MediaPlayer.create(context, R.raw.mytone);
    }

if (mPlayer.isPlaying()) {
        mPlayer.stop();
        mPlayer.release();
        mPlayer = MediaPlayer.create(context, R.raw.mytone);

}

volume = audioMan.getStreamVolume(AudioManager.STREAM_NOTIFICATION);

mPlayer.setVolume(volume, volume);//this doesn't work for me, beep sound is taking media player volume by default.

mPlayer.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer player, int what, int extra) {
            player.stop();
            player.reset();
            return true;
        }
    });

if (mVibrator == null)
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

mVibrator.cancel();

Could you please share your knowledge and show me the way. Thank you.

Solution

It looks like you’re playing your voice through a music stream referencing AudioManager.STREAM_MUSIC. Modifying the volume level modifies the level of all content playing on that stream. This is why music/media playback is “a mess”.

If you want to use ringtone streaming (and its volume settings), then you should use AudioManager.STREAM_RING instead. You say you tried this, but the code you give fragment just adjusts the volume – you don’t show how you create and configure your MediaPlayer until you ask it to play your voice.

When you set up a MediaPlayer instance, you must select the appropriate stream. Since I’ve used different streams successfully in the kind of scenario you’ve described, that’s where your problem lies. To choose to play an audio stream with a custom beep code, use setAudioStream on your MediaPlayer instance as follows

// Get a reference to the MP3 file to play
AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(R.raw.my_mp3);

 Create the media player
MediaPlayer mediaPlayer = new MediaPlayer();

 Give it the MP3 you want played
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

 Set the audio stream to play over
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

 Now play the sound
mediaPlayer.prepare();
mediaPlayer.start();

It’s good practice to give your users the option to choose their own stream – in addition to music and ringtone streams, there are alert and notification streams, each with independent volume levels (there are others, but these are the core). Check out the Android documentation for AudioManager here

Related Problems and Solutions