How to correctly set MediaPlayer audio stream type

前端 未结 1 1259
庸人自扰
庸人自扰 2020-12-03 12:12

I\'m trying to create a way to adjust volume settings for each of the different streams (media, notification, ringtone, etc) and have a way to preview the output sound level

相关标签:
1条回答
  • 2020-12-03 12:34

    Okay, I found the solution after a bit more testing and it looks like this, in case anyone else runs into the same problem I was having. The MODIFY_AUDIO_SETTINGS permission is needed in the Manifest for this to work.

    AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    am.setMode(AudioManager.MODE_NORMAL);
    MediaPlayer mp=new MediaPlayer();
    Uri ringtoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    try
    {
        mp.setDataSource(getApplicationContext(), ringtoneUri);
        mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        mp.prepare();
        mp.start();
    }
    catch(Exception e)
    {
        //exception caught in the end zone
    }
    
    0 讨论(0)
提交回复
热议问题