how to increase and decrease the volume programmatically in android

后端 未结 7 1098
余生分开走
余生分开走 2020-12-08 22:54

I created the music player app and i want to set the volume up/down programmatically .

I want to implement two another

相关标签:
7条回答
  • 2020-12-08 23:04

    Following up on solution of jesu, if you want to change volume within Service instead of Activity, replace:

    AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    

    with:

    AudioManager audioManager;
    audioManager = (AudioManager) YourServiceName.this.getSystemService(Context.AUDIO_SERVICE);
    
    0 讨论(0)
  • 2020-12-08 23:05

    This code will set the volume 100 instantly with no feedback to the user when buttonSES is pressed.

    public void perform_actionSES(View v) {
    Button buttonSES = (Button) findViewById(R.id.buttonSES);
    
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 100, 0);  
    }
    
    0 讨论(0)
  • 2020-12-08 23:12

    Create an object for audio manager

    AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    
    
    Button upButton = (Button) findViewById(R.id.upButton);
            upButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
    //To increase media player volume               
                    audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                }
            });
    
            Button downButton = (Button) findViewById(R.id.downButton);
            downButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
    //To decrease media player volume
                    audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);             
                }
            });
    

    The above example used Button label

    for volume up and down code

    @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            int action = event.getAction();
            int keyCode = event.getKeyCode();
            switch (keyCode) {
                case KeyEvent.KEYCODE_VOLUME_UP:
                    if (action == KeyEvent.ACTION_DOWN) {
                        audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                    }
                    return true;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    if (action == KeyEvent.ACTION_DOWN) {
                        audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
                    }
                    return true;
                default:
                    return super.dispatchKeyEvent(event);
            }
        }
    
    0 讨论(0)
  • 2020-12-08 23:12

    Try below code

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    
    seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    
    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int newVolume, boolean b) {
        textview.setText("Media Volume : " + newVolume);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
      }
    
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {}
    
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {}
    });
    
    0 讨论(0)
  • 2020-12-08 23:13

    Below code worked great

    There are several streams available in Android using which audio can be managed

     /** The audio stream for phone calls */
        public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
        /** The audio stream for system sounds */
        public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
        /** The audio stream for the phone ring */
        public static final int STREAM_RING = AudioSystem.STREAM_RING;
        /** The audio stream for music playback */
        public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
        /** The audio stream for alarms */
        public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
        /** The audio stream for notifications */
        public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
        /** @hide The audio stream for phone calls when connected to bluetooth */
        public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
        /** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
        public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
        /** The audio stream for DTMF Tones */
        public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
        /** @hide The audio stream for text to speech (TTS) */
        public static final int STREAM_TTS = AudioSystem.STREAM_TTS;
    

    Depending on your need you can change the stream here is STREAM_MUSIC example :

    You can get constant value using refelction from AudioSytem class:

     private int getStreamType(String streamName) {
            final String streamSourceClassName = "android.media.AudioSystem";
            int streamType = 0;
            try {
                streamType = (int) Class.forName(streamSourceClassName).getDeclaredField(streamName).get(null);
            } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
                e.printStackTrace();
            }
            return streamType;
        }
    
    
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    
    seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    
    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int newVolume, boolean b) {
        textview.setText("Media Volume : " + newVolume);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
      }
    
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {}
    
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {}
    });
    
    0 讨论(0)
  • 2020-12-08 23:24
    AudioManager aManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    
    
    Button buttonUp= (Button) findViewById(R.id.upButton);
            buttonUp.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
        enter code here
    
    //To increase media player volume               
                    `enter code here`aManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                }
            });
    
            Button downButton = (Button) findViewById(R.id.downButton);
            buttondown.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    aManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);             
                }
            });
    
    0 讨论(0)
提交回复
热议问题