无论设备音量如何,都播放声音

所以在我的2.3设备上,即使设备音量设置为0 /静音,我也可以用SoundPool或MediaPlayer以全音量播放声音。 这是我的理解,你必须手动获取设备级别,并在播放声音时进行设置。

这是我想要的行为工作。

不过,我现在注意到在我的4.0设备上,声音是在设备的设置级别自动播放的,我不想!

这是不同版本的操作系统之间的差异? 如果是这样,有没有办法忽略设备的音量? 所以即使静音,我也可以发出声音并听到它?

我不能进入为什么我需要这个function,但我真的真的这样做。

谢谢!

Solutions Collecting From Web of "无论设备音量如何,都播放声音"

我有类似的需要闹钟应用程序。 这里是相关的代码,有关量的评论。

当声音模式设置为静音时,当手动设置闹钟stream量为零,铃声音量设置为零时,这可以在我的HTC Rezound Android版本4.0.3上运行。

Context context; MediaPlayer mp; AudioManager mAudioManager; int userVolume; public AlarmController(Context c) { // constructor for my alarm controller class this.context = c; mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //remeber what the user's volume was set to before we change it. userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM); mp = new MediaPlayer(); } public void playSound(String soundURI){ Uri alarmSound = null; Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); try{ alarmSound = Uri.parse(soundURI); }catch(Exception e){ alarmSound = ringtoneUri; } finally{ if(alarmSound == null){ alarmSound = ringtoneUri; } } try { if(!mp.isPlaying()){ mp.setDataSource(context, alarmSound); mp.setAudioStreamType(AudioManager.STREAM_ALARM); mp.setLooping(true); mp.prepare(); mp.start(); } } catch (IOException e) { Toast.makeText(context, "Your alarm sound was unavailable.", Toast.LENGTH_LONG).show(); } // set the volume to what we want it to be. In this case it's max volume for the alarm stream. mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND); } public void stopSound(){ // reset the volume to what it was before we changed it. mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND); mp.stop(); mp.reset(); } public void releasePlayer(){ mp.release(); } 

从原始文件夹播放音乐的一种简单而又可选的方法;

 try { String uri = "android.resource://" + getPackageName() + "/" + R.raw.beep; //Strign uri = "http://bla-bla-bla.com/bla-bla.wav" Uri notification = Uri.parse(uri); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { e.printStackTrace(); }