Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing sounds regardless of device volume level

Tags:

android

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.

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.

Thanks!

like image 554
romamnmlst Avatar asked Oct 23 '12 23:10

romamnmlst


People also ask

What does System volume do on Android?

The system volume is the volume adjusted on the device, which is controlled by the volume type used by the Agora Video SDK. For example, if the SDK uses the in-call volume, then when you adjust the system volume, you actually adjust the in-call volume.

How do I control volume on other apps?

App Volume Control When you first open the app, you'll see a list of all the other apps that are installed on your Android device. You'll be able to easily find your desired app, since App Volume Control divides the apps into two sections: Installed Apps and System Apps. Tap on an app to adjust the volume setting.

Is there a volume widget for Android?

A-Volume is a free widget application for Android to help you control the volume levels of the alarm, media player, voice call and notifications. Also you can fully customize the widget size, color and look-and-feel.


2 Answers

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

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();
    }
like image 197
Jason Hessley Avatar answered Sep 28 '22 02:09

Jason Hessley


An easy and alternative way for playing music from raw folder;

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();
    }
like image 32
Rashid Avatar answered Sep 28 '22 03:09

Rashid