如何在静音模式下启用媒体声音

时间:2012-12-10 10:08:17

标签: android media-player

即使在静音模式下如何启动声音?我的原始文件夹中有我的音频文件。我已经制作了一些代码但是当我进入android的声音设置时,铃声和闹钟是唯一被改为最大但不是媒体部分的。这是我的代码

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 个答案:

答案 0 :(得分:5)

现在好了,我使用了这段代码

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);