Activity方法中有个一个onKeyDown方法用于实现监听音量上下键调节。
具体的代码实现:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.audio.android.volumekeydown.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id ="@+id/textView"
android:textSize = "100px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.audio.android.volumekeydown;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
tv.setText("volume|||" + count);
count--;
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
tv.setText("volume|||" + count);
count++;
return true;
}
return super.onKeyDown(keyCode, event);
}
}
按音量上下键的时候会增加或者减少数值,模拟器上运行的效果
第二个例子调用相关接口设置音量,在第一个例子的基础上进行修改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical"
tools:context="com.audio.android.volumekeydown.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="62dp"
android:text="Hello World!"
android:textSize="50px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="down"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="307dp" />
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play"
tools:layout_editor_absoluteX="278dp"
tools:layout_editor_absoluteY="307dp"
tools:ignore="MissingConstraints" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="toggleButton"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="409dp" />
<Button
android:id="@+id/up"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="up"
tools:layout_editor_absoluteX="52dp"
tools:layout_editor_absoluteY="307dp" />
</LinearLayout>
package com.audio.android.volumekeydown;
import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import static android.R.attr.onClick;
public class MainActivity extends AppCompatActivity {
private TextView tv;
int count = 0;
AudioManager mAudioManager;
Button play, up, down;
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
play = (Button) findViewById(R.id.play);
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.lemon_tree);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
});
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
});
down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
}
});
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, b);
// mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
tv.setText("volume|||" + count);
count--;
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
tv.setText("volume|||" + count);
count++;
return true;
}
return super.onKeyDown(keyCode, event);
}
}
这个例子设计了几个按钮,比如用到了前面的音乐播放,还有声音的大小的调节,还有一个mute/unmute声音的调节,不过setStreamMute已经是depreate.