android音量调节(四)android10 设置模块音量设置源码实现

本文详细解读了Android系统中如何通过设置模块中的SeekBar来控制音量,涉及SoundSettings.java中的VolumeSeekBarPreference及其Controller类,以及AudioManager和AudioService中的setStreamVolume方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


android 修改音量除了实体按键,还有在设置模块中,有专门的音量模块可以滑动滑动条(seekbar)来控制音量(界面如下图所示)。接下来我们看下源码中怎么拖拽设置音量的。

该界面布局界面加载fragment的路径:

packages/apps/Settings/res/xml/sound_settings.xml

packages/apps/Settings/src/com/android/settings/notification/SoundSettings.java

<!-- Media volume -->
    <com.android.settings.notification.VolumeSeekBarPreference
        android:key="media_volume"
        android:icon="@drawable/ic_media_stream"
        android:title="@string/media_volume_option_title"
        android:order="-180"
        settings:controller="com.android.settings.notification.MediaVolumePreferenceController"/>

    <!-- Call volume -->
    <com.android.settings.notification.VolumeSeekBarPreference
        android:key="call_volume"
        android:icon="@drawable/ic_local_phone_24_lib"
        android:title="@string/call_volume_option_title"
        android:order="-170"
        settings:controller="com.android.settings.notification.CallVolumePreferenceController"/>
    <!-- Ring volume -->
    <com.android.settings.notification.VolumeSeekBarPreference
        android:key="ring_volume"
        android:icon="@drawable/ic_notifications"
        android:title="@string/ring_volume_option_title"
        android:order="-160"
        settings:controller="com.android.settings.notification.RingVolumePreferenceController"/>


    <!-- Alarm volume -->
    <com.android.settings.notification.VolumeSeekBarPreference
        android:key="alarm_volume"
        android:icon="@*android:drawable/ic_audio_alarm"
        android:title="@string/alarm_volume_option_title"
        android:order="-150"
        settings:controller="com.android.settings.notification.AlarmVolumePreferenceController"/>

 以上是图一对应的布局(其他音量的不再此讨论,逻辑几乎是一样的)

我们看到每个布局都对应一个controller,我们以闹钟为例

1. 布局类是VolumeSeekBarPreference,逻辑类是AlarmVolumePreference。

public class AlarmVolumePreferenceController extends VolumeSeekBarPreferenceController {

    private static final String KEY_ALARM_VOLUME = "alarm_volume";

    public AlarmVolumePreferenceController(Context context) {
        super(context, KEY_ALARM_VOLUME);
    }

    @Override
    public int getAvailabilityStatus() {
        return mContext.getResources().getBoolean(R.bool.config_show_alarm_volume)
                && !mHelper.isSingleVolume() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    @Override
    public boolean isSliceable() {
        return TextUtils.equals(getPreferenceKey(), "alarm_volume");
    }

    @Override
    public boolean useDynamicSliceSummary() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return KEY_ALARM_VOLUME;
    }

    @Override
    public int getAudioStream() {
        return AudioManager.STREAM_ALARM;
    }

    @Override
    public int getMuteIcon() {
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值