how to enable media sound even in silent mode

十年热恋 提交于 2019-12-10 10:01:09

问题


how do i start the sound even in silent mode? i got my audio file in my raw folder. i've made some codes but when i go to sound settings of android the ringtone and alarm are the only ones that are changed to maximum but not the media part. here is my code

public void playSound(Context context)
{
    AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);;
    MediaPlayer mMediaPlayer;
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer = MediaPlayer.create(context, R.raw.puff);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(false);
    mMediaPlayer.start();
}

回答1:


it's okay now, i used this code

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    int maxVolumeMusic = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolumeMusic,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);


来源:https://stackoverflow.com/questions/13798758/how-to-enable-media-sound-even-in-silent-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!