3

即使在静音模式下如何启动声音?我的原始文件夹中有我的音频文件。我已经编写了一些代码,但是当我进入 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();
}
4

1 回答 1

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);
于 2012-12-10T10:28:51.320 回答