如何在 Android 中以编程方式增加和减少音量

2022-09-02 00:02:09

我创建了一个音乐播放器应用程序,我想以编程方式设置音量。我想实现两个s来增加/减少音量并设置为媒体播放器。Button

活动

control = (ImageView) findViewById(R.id.control);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
control.setOnClickListener(pausePlay);
control.setBackgroundResource(R.drawable.pause);
control id is my play and pause button :
{
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub

        if (playPause) {
            control.setBackgroundResource(R.drawable.play);
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
            media.stop();
            intialStage = false;
            playPause = false;

        } else {
            control.setBackgroundResource(R.drawable.pause);
            if (intialStage) {
                new Player()
                        .execute("http://streaming.shoutcast.com/MUKILFMRADIO");
            } else {
                if (!mediaPlayer.isPlaying())
                    mediaPlayer.start();
            }
            playPause = true;
        }
}

布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/control1"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:background="@drawable/decrement"
        android:layout_above="@+id/latestAddedSongs"
        android:layout_alignEnd="@+id/musicArtistName" />
    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/control"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:background="@drawable/play"
        android:layout_above="@+id/latestAddedSongs"
        android:layout_alignEnd="@+id/musicArtistName" />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/control2"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:background="@drawable/increment"
        android:layout_above="@+id/latestAddedSongs"
        android:layout_alignEnd="@+id/musicArtistName" />
</LinearLayout>

答案 1

为音频管理器创建对象

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

上面的示例使用了按钮标签

用于音量增大和调低代码

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

答案 2

试试下面的代码

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

推荐