Android系统的音量控制

简介: Android系统的音量控制效果图:GitHubGitHub(源码):https://github.

Android系统的音量控制

效果图:

G2

GitHub

GitHub(源码):https://github.com/kongqw/VolumeController

方法

  • 获取AudioManager

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  • 获取最大音量

    mAudioManager.getStreamMaxVolume(`streamType`);
  • 获取当前音量

    mAudioManager.getStreamVolume(`streamType`);
  • 设置音量

    mAudioManager.setStreamVolume(`streamType`, `音量大小`, `flags`);
  • streamType几种类型

    1. AudioManager.STREAM_MUSIC (媒体音量)
    2. AudioManager.STREAM_VOICE_CALL (通话音量)
    3. AudioManager.STREAM_ALARM (闹钟音量)
    4. AudioManager.STREAM_RING (铃声音量)
    5. ……
  • flags几种类型

    1. AudioManager.FLAG_PLAY_SOUND (声音)
    2. AudioManager.FLAG_SHOW_UI (UI)
    3. AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE (移除声音和震动)
    4. ……

Code

package kong.qingwei.volumecontroller;

import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener {

    private AudioManager mAudioManager;
    private SwitchCompat mSwitchVolume;
    private SwitchCompat mSwitchUi;
    private int flag = AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI;

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

        mSwitchVolume = (SwitchCompat) findViewById(R.id.switch_volume);
        if (mSwitchVolume != null) {
            mSwitchVolume.setOnCheckedChangeListener(this);
        }
        mSwitchUi = (SwitchCompat) findViewById(R.id.switch_ui);
        if (mSwitchUi != null) {
            mSwitchUi.setOnCheckedChangeListener(this);
        }

        // AudioManager
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        // 媒体音量
        int mMusicMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar seekBar_music = (SeekBar) findViewById(R.id.seekBar_music);
        if (seekBar_music != null) {
            seekBar_music.setMax(mMusicMaxVolume);
            seekBar_music.setProgress(mCurrentVolume);
            seekBar_music.setOnSeekBarChangeListener(this);
        }

        // 通话音量
        int mCallMaxVolume1 = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        int mCallCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
        SeekBar seekBar_call = (SeekBar) findViewById(R.id.seekBar_call);
        if (seekBar_call != null) {
            seekBar_call.setMax(mCallMaxVolume1);
            seekBar_call.setProgress(mCallCurrentVolume);
            seekBar_call.setOnSeekBarChangeListener(this);
        }

        // 闹钟音量
        int mAlarmMaxVolume1 = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        int mAlarmCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        SeekBar seekBar_alarm = (SeekBar) findViewById(R.id.seekBar_alarm);
        if (seekBar_alarm != null) {
            seekBar_alarm.setMax(mAlarmMaxVolume1);
            seekBar_alarm.setProgress(mAlarmCurrentVolume);
            seekBar_alarm.setOnSeekBarChangeListener(this);
        }

        // 铃声音量
        int mRingMaxVolume1 = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
        int mRingCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
        SeekBar seekBar_ring = (SeekBar) findViewById(R.id.seekBar_ring);
        if (seekBar_ring != null) {
            seekBar_ring.setMax(mRingMaxVolume1);
            seekBar_ring.setProgress(mRingCurrentVolume);
            seekBar_ring.setOnSeekBarChangeListener(this);
        }

        //        // 铃声音量
        //        int mNotificationMaxVolume1 = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
        //        int mNotificationCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        //        SeekBar seekBar_notification = (SeekBar) findViewById(R.id.seekBar_notification);
        //        assert seekBar_notification != null;
        //        seekBar_notification.setMax(mNotificationMaxVolume1);
        //        seekBar_notification.setProgress(mNotificationCurrentVolume);
        //        seekBar_notification.setOnSeekBarChangeListener(this);
        //
        //        // 系统音量
        //        int mSystemMaxVolume1 = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        //        int mSystemCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        //        SeekBar seekBar_system = (SeekBar) findViewById(R.id.seekBar_system);
        //        assert seekBar_system != null;
        //        seekBar_system.setMax(mSystemMaxVolume1);
        //        seekBar_system.setProgress(mSystemCurrentVolume);
        //        seekBar_system.setOnSeekBarChangeListener(this);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.seekBar_music: // 媒体音量
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, flag);
                break;
            case R.id.seekBar_call: // 通话音量
                mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, progress, flag);
                break;
            case R.id.seekBar_alarm: // 闹钟音量
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, flag);
                break;
            case R.id.seekBar_ring: // 铃声音量
                mAudioManager.setStreamVolume(AudioManager.STREAM_RING, progress, flag);
                break;
            //            case R.id.seekBar_notification: // 铃声音量
            //                mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            //                break;
            //            case R.id.seekBar_system: // 系统音量
            //                mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, progress, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            //                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_volume:
                if (isChecked) {
                    if (mSwitchUi.isChecked()) {
                        flag = AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI;
                    } else {
                        flag = AudioManager.FLAG_PLAY_SOUND;
                    }
                } else {
                    if (mSwitchUi.isChecked()) {
                        flag = AudioManager.FLAG_SHOW_UI;
                    } else {
                        flag = AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE;
                    }
                }
                break;
            case R.id.switch_ui:
                if (isChecked) {
                    if (mSwitchVolume.isChecked()) {
                        flag = AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI;
                    } else {
                        flag = AudioManager.FLAG_SHOW_UI;
                    }
                } else {
                    if (mSwitchVolume.isChecked()) {
                        flag = AudioManager.FLAG_PLAY_SOUND;
                    } else {
                        flag = AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE;
                    }
                }
                break;
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kong.qingwei.volumecontroller.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="声音" />

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/switch_volume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:checked="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:text="界面" />

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/switch_ui"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:checked="true" />


    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="媒体音量" />

    <SeekBar
        android:id="@+id/seekBar_music"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="通话音量" />

    <SeekBar
        android:id="@+id/seekBar_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="闹钟音量" />

    <SeekBar
        android:id="@+id/seekBar_alarm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="铃声音量" />

    <SeekBar
        android:id="@+id/seekBar_ring"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <!--<TextView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:text="铃声音量" />-->

    <!--<SeekBar-->
    <!--android:id="@+id/seekBar_notification"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_centerHorizontal="true" />-->

    <!--<TextView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:text="系统音量" />-->

    <!--<SeekBar-->
    <!--android:id="@+id/seekBar_system"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_centerHorizontal="true" />-->
</LinearLayout>
相关文章
|
1月前
|
Android开发
Android 如何将定制的Launcher成为系统中唯一的Launcher
Android 如何将定制的Launcher成为系统中唯一的Launcher
46 2
|
11天前
|
存储 监控 调度
Android系统服务:WMS、AMS相关知识
参考文献 Android窗口管理服务WindowManagerService计算Activity窗口大小的过程分析 Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析 Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析 Android窗口管理服务WindowManagerService显示窗口动画的原理分析
|
15天前
|
Java Linux Android开发
Android面试题之说说系统的启动流程(总结)
这篇文章概述了Android系统的启动流程,从Boot Rom到Zygote进程和SystemServer的启动。init进程作为用户级别的第一个进程,负责创建文件目录、初始化服务并启动Zygote。Zygote通过预加载资源和创建Socket服务,使用fork函数生成SystemServer进程。fork过程中,子进程继承父进程大部分信息但具有独立的进程ID。Zygote预加载资源以减少后续进程的启动时间,而SystemServer启动众多服务并最终开启Launcher应用。文中还讨论了为何从Zygote而非init或SystemServer fork新进程的原因。
22 2
|
22天前
|
JavaScript Java 测试技术
基于ssm+vue.js+uniapp小程序的安卓的微博客系统附带文章和源代码部署视频讲解等
基于ssm+vue.js+uniapp小程序的安卓的微博客系统附带文章和源代码部署视频讲解等
26 2
|
3天前
|
安全 搜索推荐 Android开发
探索安卓和iOS系统的优劣与特点
在移动操作系统领域,安卓和iOS一直是最热门的两个选择。本文将探讨安卓和iOS系统的优劣与特点,帮助读者更好地了解这两个操作系统,并为选择合适的移动设备提供参考。
11 0
|
1月前
|
Android开发
【通讯录教程】苹果安卓鸿蒙系统通用,如何大批量导入手机号码到手机的通讯录,下面教你方法,只需1分钟搞定几万个号码的导入手机电话本
该文介绍了一种快速批量导入手机通讯录的方法,适用于处理大量手机号的需求,如微商管理、客户资料整理等。在QQ同步助手开始收费后,提供了免费的替代方案。步骤包括:下载批量导入软件(链接提供腾讯云盘和百度网盘地址),清空通讯录(非必需),制作符合格式的通讯录文件,并按操作系统(苹果、安卓或鸿蒙)进行导入。整个过程只需1分钟,简便快捷。
|
14天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的安卓的微博客系统的详细设计和实现
基于SpringBoot+Vue+uniapp的安卓的微博客系统的详细设计和实现
11 0
|
1月前
|
安全 搜索推荐 物联网
构建未来:基于Android的智能物联网家居系统
【5月更文挑战第15天】 在快速发展的数字化时代,智能物联网(IoT)技术与移动操作系统的结合正在塑造未来家居的生活方式。特别是Android平台,以其开放性、灵活性和广泛的用户基础,成为智能家居创新的理想选择。本文将探讨如何利用Android系统构建一个高效、安全且易于扩展的智能家居控制系统,涵盖系统设计、关键技术实现以及可能面临的挑战。通过分析具体案例,我们旨在为开发者和企业提供一套可行的解决方案蓝图,以促进智能家居领域的进一步发展。
|
1月前
|
存储 缓存 Android开发
Android系统分区与升级
Android系统分区与升级
57 4
|
1月前
|
Android开发
Android获取当前系统日期和时间的三种方法
Android获取当前系统日期和时间的三种方法
43 4