【问题标题】:Playing sounds regardless of device volume level无论设备音量如何播放声音
【发布时间】:2012-10-23 23:00:55
【问题描述】:

所以在我的 2.3 设备上,我可以使用 SoundPool 或 MediaPlayer 以最大音量播放声音,即使设备音量设置为 0/静音。据我了解,您必须在播放声音时手动获取设备级别并进行设置。

这就是我希望行为起作用的方式。

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

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

我不知道为什么我需要这个功能,但我真的需要。

谢谢!

【问题讨论】:

  • 我觉得你真的应该解释一下为什么你必须拥有这个功能。我唯一想到的是某种丢失的手机定位应用程序。
  • 我真的不能。我已经为这个项目签署了 NDA,但这是一个主要功能。如果我不能强制声音忽略系统音量,我需要进行一些重大的重新设计。

标签: android


【解决方案1】:

我对闹钟应用程序也有类似的需求。这是与 cmets 有关卷的相关代码。

当声音配置文件设置为静音、警报流音量手动设置为零以及铃声音量设置为零时,这适用于我的 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();
    }

【讨论】:

  • 非常感谢您的这...完美!
  • 谢谢你为我节省了很多时间!
  • 请注意,您可能需要将 AudioManager.FLAG_PLAY_SOUND 更改为 0 以避免声音更改蜂鸣声。
【解决方案2】:

一种从原始文件夹播放音乐的简单替代方法;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2018-11-20
    相关资源
    最近更新 更多