Android Studio 基础 之 获取各个音量的最大值、最小值、以及当前音量值,并设置音量值,监听音量变化的方法整理
目录
Android Studio 基础 之 获取各个音量的最大值、最小值、以及当前音量值,并设置音量值的方法整理
一、简单介绍
二、实现原理
三、使用注意
四、效果预览
五、实现步骤,
六、关键代码
一、简单介绍
Android 开发中的一些基础操作,使用整理,便于后期使用。
本节介绍,Android 开发中,获取手机设备的各种音量值,最大最小当前值什么的,最后举例设置音乐音量值的方法(按音量上下键设置的那个音量),方法不唯一,欢迎指正。
二、实现原理
1、 getSystemService(Context.AUDIO_SERVICE) 获得音频管理 AudioManager
2、根据 AudioManager.getStreamMaxVolume(AudioManager.STREAM_XXX) 得到各种音量的最大值
3、根据 AudioManager.getStreamMinVolume(AudioManager.STREAM_XXX) 得到各种音量的最小值
4、根据 AudioManager.getStreamVolume(AudioManager.STREAM_XXX) 得到各种音量的当前值
5、根据 AudioManager.setStreamVolume(AudioManager.STREAM_XXX,Value,AudioManager.FLAG_XXX) 设置各个音量值
6、intent.getAction().equals(ACTION_VOLUME_CHANGED) 广播监听音量变化
三、使用注意
1、AudioManager.getStreamMinVolume(AudioManager.STREAM_XXX) 得到各种音量的最小值,好像 minSdkVersion 需要 minSdkVersion 28
四、效果预览
五、实现步骤,
1、打开 Android Studio 新建一个工程,或者新建一个模块
2、默认操作,一路创建一个模块
3、创建一个 VoiceVolumnWrapper ,编写获取各个音量最大最小当前值,和设置当前值,广播监听音量变化的方法
4、在 MainActivity 中添加,测试 VoiceVolumnWrapper 接口的方法,然后设置一个超大值,看是否能保持原最大值
5、打包,在设备上运行的结果如下
六、关键代码
1、VoiceVolumnWrapper
package com.example.voicevolumntest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import java.lang.ref.WeakReference;
public class VoiceVolumnWrapper {
private static final String TAG = "Main_VoiceVolumnWrapper";
private AudioManager _AudioManager;
private Context _Context;
private static final String ACTION_VOLUME_CHANGED = "android.media.VOLUME_CHANGED_ACTION";
private static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE";
private MyVolumeReceiver mVolumeReceiver;
public VoiceVolumnWrapper(Context context){
_Context = context;
_AudioManager =(AudioManager) _Context.getSystemService(Context.AUDIO_SERVICE);
}
/**
* // 通话音量
* @return
*/
public int GetCallVoiceMax(){
return _AudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL) ;
}
/**
* // 通话音量
* @return
*/
public int GetCallVoiceMin(){
return _AudioManager.getStreamMinVolume(AudioManager.STREAM_VOICE_CALL) ;
}
/**
* // 通话音量
* @return
*/
public int GetCallVoiceCurrentValue(){
return _AudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) ;
}
/**
* // 系统音量
* @return
*/
public int GetSystemVoiceMax(){
return _AudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM) ;
}
/**
* // 系统音量
* @return
*/
public int GetSystemVoiceMin(){
return _AudioManager.getStreamMinVolume(AudioManager.STREAM_SYSTEM) ;
}
/**
* // 系统音量
* @return
*/
public int GetSystemVoiceCurrentValue(){
return _AudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM) ;
}
/**
* // 铃声音量
* @return
*/
public int GetRingVoiceMax(){
return _AudioManager.getStreamMaxVolume(AudioManager.STREAM_RING) ;
}
/**
* // 铃声音量
* @return
*/
public int GetRingVoiceMin(){
return _AudioManager.getStreamMinVolume(AudioManager.STREAM_RING) ;
}
/**
* // 铃声音量
* @return
*/
public int GetRingVoiceCurrentValue(){
return _AudioManager.getStreamVolume(AudioManager.STREAM_RING) ;
}
/**
* // 音乐音量
* @return
*/
public int GetMusicVoiceMax(){
return _AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) ;
}
/**
* // 音乐音量
* @return
*/
public int GetMusicVoiceMin(){
return _AudioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC) ;
}
/**
* // 音乐音量
* @return
*/
public int GetMusicVoiceCurrentValue(){
return _AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) ;
}
/**
* 增加音乐音量
* @param value
*/
public void AddMusicVoiceVolumn(int value){
int addValue = (GetMusicVoiceCurrentValue() + value) ;
// 防止音量值越界
addValue = addValue > GetMusicVoiceMax() ?GetMusicVoiceMax():addValue;
_AudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,addValue,AudioManager.FLAG_PLAY_SOUND);
}
/**
* 减少音乐音量
* @param value
*/
public void ReduceMusicVoiceVolumn(int value){
int reduceValue = (GetMusicVoiceCurrentValue() - value) ;
// 防止音量值越界
reduceValue = reduceValue < GetMusicVoiceMin() ?GetMusicVoiceMin():reduceValue;
_AudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,reduceValue,AudioManager.FLAG_PLAY_SOUND);
}
/**
* 注册广播监听
*/
public void registerVolumeReceiver() {
mVolumeReceiver = new MyVolumeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.media.VOLUME_CHANGED_ACTION");
_Context.registerReceiver(mVolumeReceiver, filter);
}
/**
* 取消注册广播监听
*/
public void unregisterVolumeReceiver() {
if (mVolumeReceiver != null) _Context.unregisterReceiver(mVolumeReceiver);
}
/**
* 音量变化广播类
*/
private class MyVolumeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isReceiveVolumeChange(intent) == true) {
int currVolume = _AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Toast.makeText(context, currVolume + " ", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onReceive:isReceiveVolumeChange currVolume "+currVolume);
}
}
}
/**
* 判断是否是音乐音量变化(音量键改变的音量)
* @param intent
* @return
*/
private boolean isReceiveVolumeChange(Intent intent) {
return intent.getAction() != null
&& intent.getAction().equals(ACTION_VOLUME_CHANGED)
&& intent.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC
;
}
}
2、MainActivity
package com.example.voicevolumntest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
VoiceVolumnWrapper _VoiceVolumnWrapper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_VoiceVolumnWrapper = new VoiceVolumnWrapper(this);
Log.i(TAG, "onCreate:Call Voice " + _VoiceVolumnWrapper.GetCallVoiceMax() +" " + _VoiceVolumnWrapper.GetCallVoiceMin() +" "+ _VoiceVolumnWrapper.GetCallVoiceCurrentValue());
Log.i(TAG, "onCreate:System Voice " + _VoiceVolumnWrapper.GetSystemVoiceMax() +" " + _VoiceVolumnWrapper.GetSystemVoiceMin() +" "+ _VoiceVolumnWrapper.GetSystemVoiceCurrentValue());
Log.i(TAG, "onCreate:Ring Voice " + _VoiceVolumnWrapper.GetRingVoiceMax() +" " + _VoiceVolumnWrapper.GetRingVoiceMin() +" "+ _VoiceVolumnWrapper.GetRingVoiceCurrentValue());
Log.i(TAG, "onCreate:Music Voice " + _VoiceVolumnWrapper.GetMusicVoiceMax() +" " + _VoiceVolumnWrapper.GetMusicVoiceMin() +" "+ _VoiceVolumnWrapper.GetMusicVoiceCurrentValue());
_VoiceVolumnWrapper.registerVolumeReceiver();
_VoiceVolumnWrapper.AddMusicVoiceVolumn(100);
Log.i(TAG, "onCreate:Music Voice " + _VoiceVolumnWrapper.GetMusicVoiceMax() +" " + _VoiceVolumnWrapper.GetMusicVoiceMin() +" "+ _VoiceVolumnWrapper.GetMusicVoiceCurrentValue());
_VoiceVolumnWrapper.ReduceMusicVoiceVolumn(300);
Log.i(TAG, "onCreate:Music Voice " + _VoiceVolumnWrapper.GetMusicVoiceMax() +" " + _VoiceVolumnWrapper.GetMusicVoiceMin() +" "+ _VoiceVolumnWrapper.GetMusicVoiceCurrentValue());
}
@Override
protected void onDestroy() {
_VoiceVolumnWrapper.unregisterVolumeReceiver();
super.onDestroy();
}
}
3、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.voicevolumntest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>