播放设备的音量水平的声音无论 [英] Playing sounds regardless of device volume level

查看:132
本文介绍了播放设备的音量水平的声音无论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在我的2.3设备,我可以以最大音量播放用的Soundpool或MediaPlayer的声音,即使设备音量设置为0 /静音。这是我的理解是,你必须手动获取设备的水平,并设置它,当你播放的声音。

So on my 2.3 devices, I can play a sound with SoundPool or MediaPlayer at full volume, even if the device volume is set to 0/mute. It was my understanding that you had to manually get the device level and set it when you played back a sound.

这是我想要的行为工作。

This is how I want the behavior to work.

不过,我现在发现我4.0的设备,使声音在设备的设定值,这是我不希望自动播放!

However, I now notice on my 4.0 device, that the sounds are automatically played at the device's set level, which I do not want!

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

Is this a difference between versions of the OS? If so, is there a way to ignore the devices volume? So even if it is muted, I can play a sound and have it be heard?

我不能进入我为什么需要这个功能,但是我真的是认真的。

I can't go into why I need this feature, but I really really do.

谢谢!

推荐答案

我也有类似的需要闹钟应用程序。下面是相关code提供有关该卷的意见。

I had a similar need for an alarm clock application. Here is the relevant code with comments regarding the volume.

这适用于我的HTC Rezound安卓版4.0.3当声音情景设置为静音,当报警流音量手动设定为零,当铃声音量设置为零。

This works on my HTC Rezound Android Version 4.0.3 when the sound profile is set to silent, when the alarm stream volume is manually set to zero and when the ringtone volume is set to zero.

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

这篇关于播放设备的音量水平的声音无论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆