欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android音量调节

程序员文章站 2022-12-03 09:43:31
Android音量调节1、准备工作2、开始调节3、兼用安卓7.0以上全代码1、准备工作//初始化音频管理器AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);//获取系统最大音量int maxVolume=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 获取设备当前音量int currentVolume =mAudio...

1、准备工作

//初始化音频管理器
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
//获取系统最大音量
int maxVolume=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)// 获取设备当前音量
int currentVolume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// 设置seekbar的最大值
sbVolume.setMax(maxVolume);
// 显示音量
sbVolume.setProgress(currentVolume);

2、开始调节

调节音量有两种方式一种是渐进式,即像手动按音量键一样,一步一步增加或减少,另一种是直接设置音量值

  1. 渐进式
public void adjustStreamVolume (int streamType, int direction, int flags)
//减少音量
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FX_FOCUS_NAVIGATION_UP);
//增加电量
mAudioManager.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,AudioManager.FX_FOCUS_NAVIGATION_UP)

第一个streamType是需要调整音量的类型,这里设的是媒体音量,可以是:

STREAM_ALARM 警报
STREAM_MUSIC 音乐回放即媒体音量
STREAM_NOTIFICATION 窗口顶部状态栏Notification,
STREAM_RING 铃声
STREAM_SYSTEM 系统
STREAM_VOICE_CALL 通话
STREAM_DTMF 双音多频,不是很明白什么东西

第二个direction,是调整的方向,增加或减少,可以是:

ADJUST_LOWER 降低音量
ADJUST_RAISE 升高音量
ADJUST_SAME 保持不变,这个主要用于向用户展示当前的音量

第三个flags是一些附加参数,只介绍两个常用的

FLAG_PLAY_SOUND 调整音量时播放声音

FLAG_SHOW_UI 调整时显示音量条,就是按音量键出现的那个
  1. 直接设置
public void setStreamVolume (int streamType, int index, int flags)
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND);
//得到听筒模式的最大值
mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
//得到听筒模式的当前值
mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);

3、兼用安卓7.0以上全代码

  1. Activity
package com.example.android_text;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;

import com.example.android_text.common.BaseActivity;

public class SoundSettingsActivity extends BaseActivity {

    AudioManager mAudioManager;
    SeekBar linShengBar;
    SeekBar tongHuaBar;
    private static  int REQUEST_CODE_READ_CONTACTS= 123;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound_settings);

        //获取系统的声音设置
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        linShengBar=findViewById(R.id.linShengBar);
        tongHuaBar=findViewById(R.id.tongHuaBar);
        initView();
        qx();
    }

    private void initView() {
       /*//系统音量
        max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
        current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
        Log.d("SYSTEM", "max : " + max + " current : " + current);

        //音乐音量
        max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
        current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
        Log.d("MUSIC", "max : " + max + " current : " + current);

        //提示声音音量
        max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
        current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
        Log.d("ALARM", "max : " + max + " current : " + current);*/



        //通话音量
        int tongMax = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
        int tongCurrent = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
        Log.d("VIOCE_CALL", "max : " + tongMax + " current : " + tongCurrent);

        tongHuaBar.setMax(tongMax);//tongHuaBar最大通话声音值
        tongHuaBar.setProgress(tongCurrent);//设置当前声音值
        tongHuaBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                Log.i("SoundSettingsActivity", "tongHuaBar  i: "+i+"     b:"+b);
                mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, i, 0);

                int tongMax = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
                int tongCurrent = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
                Log.d("VIOCE_CALL", "max : " + tongMax + " current : " + tongCurrent);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });




        //铃声音量
        int linMax = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
        int linCurrent = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
        Log.d("RING", "max : " + linMax + " current : " + linCurrent);

        linShengBar.setMax(linMax);
        linShengBar.setProgress(linCurrent);
       linShengBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           @Override
           public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

               Log.i("SoundSettingsActivity", "linShengBar: i: "+i+"     b:"+b);
               mAudioManager.setStreamVolume(AudioManager.STREAM_RING, i, 0);

               int linMax = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
               int linCurrent = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
               Log.d("RING", "max : " + linMax + " current : " + linCurrent);
           }

           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {

           }

           @Override
           public void onStopTrackingTouch(SeekBar seekBar) {

           }
       });

    }

    private void qx(){
        //android7.0以上  当您运行startActivity()时,Android会打开您的应用的免打扰访问设置.
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && !notificationManager.isNotificationPolicyAccessGranted()) {
            Intent intent = new Intent(
                    android.provider.Settings
                            .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            startActivity(intent);
        }

        //获取权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && ActivityCompat.checkSelfPermission(SoundSettingsActivity.this,
                Manifest.permission.READ_CONTACTS)
                == PackageManager.PERMISSION_DENIED) {

            ActivityCompat.requestPermissions(SoundSettingsActivity.this,
                    new String[]{ Manifest.permission.READ_CONTACTS },
                    REQUEST_CODE_READ_CONTACTS);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i("abcd", "requestCode:"+requestCode);
        if(requestCode==REQUEST_CODE_READ_CONTACTS){
            Log.i("abcd", "以获取权限");
        }
    }

}
  1. xml
<?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"
    android:gravity="center"
    tools:context=".SoundSettingsActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="铃声音量"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="(1)-"/>
        <SeekBar
            android:id="@+id/linShengBar"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="(2)+"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="通话音量"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="(4)-"/>
        <SeekBar
            android:id="@+id/tongHuaBar"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="(5)+"/>
    </LinearLayout>
</LinearLayout>

本文地址:https://blog.csdn.net/weixin_44442049/article/details/107386066