Android 中的音频管理器

androidmobile developmentapps/applications更新于 2026/2/9 1:37:17

Android 中的音频管理器是什么?

音频管理器,顾名思义,用于管理 Android 应用程序中的音频配置文件。它通常用于在 Android 设备的不同音频模式之间切换,例如铃声模式、静音模式和振动模式。

音频管理器的实现

我们将创建一个简单的应用程序,其中只显示 3 个按钮和 2 个文本视图。Android 应用程序中的 3 个按钮用于在不同的音频模式之间切换,例如铃声模式、静音模式和振动模式。我们将在文本视图中显示当前激活的模式,以通知用户设备当前激活了哪种模式。我们将按照分步指南在 Android 应用程序中实现 Toast 消息。

步骤 1:在 Android Studio 中创建新项目

如下图所示,导航到 Android Studio。在下方屏幕中,点击"新建项目"以创建一个新的 Android Studio 项目。

点击"新建项目"后,您将看到以下屏幕。

在此屏幕中,我们只需选择"空活动",然后点击"下一步"。点击"下一步"后,您将看到以下屏幕。

在此屏幕中,我们只需指定项目名称。然后会自动生成包名。

注意− 确保选择 Kotlin 作为语言。

完成所有设置后,点击"完成"即可创建一个新的 Android Studio 项目。

项目创建完成后,我们将看到两个打开的文件,即 activity_main.xml 和 MainActivity.kt 文件。

步骤 2:在 drawable 文件夹中添加图片

导航至 app>drawable>右键单击它>新建>矢量素材>点击剪贴画图标,然后搜索您要添加到应用程序中的图标。然后,只需更改图标的名称,并将该图标添加到 drawable 文件夹中即可。

步骤 3:使用 activity_main.xml

导航至 activity_main.xml。如果此文件不可见。打开此文件。在左侧窗格中导航至 app>res>layout>activity_main.xml 以打开此文件。打开此文件后,将以下代码添加到其中。代码中添加了注释,以便您详细了解。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity"> <!-- creating a simple text view --> <TextView android:id="@+id/idTVHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/idTVMode" android:layout_marginStart="5dp" android:layout_marginTop="5dp" android:layout_marginEnd="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:padding="5dp" android:text="Audio Manager in Android" android:textAlignment="center" android:textColor="#FF000000" android:textSize="18sp" android:textStyle="bold" /> <!-- creating a text view for displaying current audio mode--> <TextView android:id="@+id/idTVMode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="5dp" android:gravity="center" android:padding="5dp" android:text="Current RingTone Mode" android:textAlignment="center" android:textColor="#FF000000" android:textSize="18sp" android:textStyle="bold" /> <!-- creating a linear layout for three buttons--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/idTVMode" android:layout_margin="10dp" android:orientation="horizontal" android:weightSum="3"> <!-- creating a linear layout for our General button--> <LinearLayout android:id="@+id/idLLGeneral" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:layout_weight="1" android:background="@color/black" android:orientation="vertical" android:padding="3dp"> <!-- adding image view for our general mode--> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:padding="3dp" android:src="@drawable/ic_general" app:tint="@color/white" /> <!-- adding text view for the general mode--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="3dp" android:padding="4dp" android:text="General" android:textAlignment="center" android:textColor="@color/white" android:textStyle="bold" /> </LinearLayout> <!-- creating a linear layout for our Silent button--> <LinearLayout android:id="@+id/idLLSilent" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:layout_weight="1" android:background="@color/black" android:orientation="vertical" android:padding="3dp"> <!-- adding image view for our silent mode--> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:padding="3dp" android:src="@drawable/ic_silent" app:tint="@color/white" /> <!-- adding text view for the silent mode--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="3dp" android:padding="4dp" android:text="Silent" android:textAlignment="center" android:textColor="@color/white" android:textStyle="bold" /> </LinearLayout> <!-- creating a linear layout for our Vibrate button--> <LinearLayout android:id="@+id/idLLVibrate" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:layout_weight="1" android:background="@color/black" android:orientation="vertical" android:padding="3dp"> <!-- adding image view for our vibrate mode--> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:padding="3dp" android:src="@drawable/ic_vibrate" app:tint="@color/white" /> <!-- adding text view for the vibrate mode--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="3dp" android:padding="4dp" android:text="Vibrate" android:textAlignment="center" android:textColor="@color/white" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </RelativeLayout>

说明 − 在上面的代码中,我们创建了一个相对布局作为根布局。我们可以借助 ID 和参数来相对对齐此布局中的所有元素。

在这个相对布局中,我们创建了一个简单的文本视图,用于显示应用程序的标题。

在这个文本视图之后,我们再创建另一个文本视图,用于显示设备中当前激活的音频模式。

在这个文本视图之后,我们在一个水平线性布局中以按钮的形式显示三个垂直线性布局,用于更改设备的音频模式。借助此线性布局,我们可以将应用程序中的音频模式从常规模式切换到静音、振动等模式。

步骤 4:使用 MainActivity.kt

导航到 MainActivity.kt。如果此文件不可见,请打开此文件。在左侧窗格中,导航至 app>java>您的应用包名称>MainActivity.kt 并打开此文件。打开此文件后,添加以下代码。代码中添加了注释,方便您详细了解。

package com.example.gptapp import android.app.NotificationManager import android.content.Context import android.content.Intent import android.media.AudioManager import android.os.Build import android.os.Bundle import android.provider.Settings import android.widget.LinearLayout import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // creating variables for 3 linear layout and text view. lateinit var silentLL: LinearLayout lateinit var generalLL: LinearLayout lateinit var vibrateLL: LinearLayout lateinit var modeTV: TextView // creating class variable for audio manager class. private var audioManager: AudioManager? = null // current mode to store integer value of ringer mode. var currentmode = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // initializing variables on below line. silentLL = findViewById(R.id.idLLSilent) generalLL = findViewById(R.id.idLLGeneral) vibrateLL = findViewById(R.id.idLLVibrate) modeTV = findViewById(R.id.idTVMode) // initializing audio manager class on below line audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager? // setting current mode to ringer mode on below line. currentmode = audioManager!!.ringerMode // updating text view for different modes on below line. when (currentmode) { AudioManager.RINGER_MODE_NORMAL -> modeTV.setText("Ringer Mode") AudioManager.RINGER_MODE_SILENT -> modeTV.setText("Silent Mode") AudioManager.RINGER_MODE_VIBRATE -> modeTV.setText("Vibrate Mode") else -> modeTV.setText("Fail to get mode") } // adding click listener for silent linear layout. silentLL.setOnClickListener { // creating and initializing variable for notification manager. val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager // on below line we are checking if notification policy access is granted or not. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !notificationManager.isNotificationPolicyAccessGranted) { // if the permission is not granted we are opening an intent to accept the policy. val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS) startActivity(intent) } // on below line set ringer mode here will sets your ringer mode to silent mode audioManager!!.ringerMode = AudioManager.RINGER_MODE_SILENT // on below line displaying a toast message Toast.makeText(this@MainActivity, "Silent Mode Activated..", Toast.LENGTH_SHORT).show() // on below line setting text to mode text view as silent mode. modeTV.setText("Silent Mode Activated..") } // adding click listener for general linear layout generalLL.setOnClickListener { // set ringer mode here will sets your ringer mode to normal mode on below line. audioManager!!.setRingerMode(AudioManager.RINGER_MODE_NORMAL); // displaying toast message on below line. Toast.makeText(this@MainActivity, "Ringtone Mode Activated..", Toast.LENGTH_SHORT) .show(); // on below line setting text to mode text view as ringtone mode. modeTV.setText("Ringtone Mode Activated.."); } // adding click listener for vibrate linear layout vibrateLL.setOnClickListener { // set ringer mode here will sets your ringer mode to vibrate on below line. audioManager!!.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); // displaying toast message on below line. Toast.makeText(this@MainActivity, "Vibrate Mode Activated..", Toast.LENGTH_SHORT) .show(); // on below line setting text to mode text view as vibrate mode is activated modeTV.setText("Vibrate Mode Activated.."); } } }

说明 - 在 MainActivity.kt 文件的上述代码中,首先,我们为已创建的不同视图(例如文本视图和线性布局)创建一个变量。然后,我们为音频管理器类创建一个变量并将其初始化为 null。之后,我们为当前模式创建一个变量,值为 0,用于选择当前音频模式。

现在,在 onCreate 方法中,我们使用在 activity_main.xml 文件中指定的唯一 ID 来初始化线性布局和文本视图变量。

初始化视图后,我们通过调用 get 系统服务并传递要使用的服务名称来初始化音频管理器变量。我们将使用音频服务来管理应用程序中的音频模式。然后,我们初始化当前模式变量,并在其中设置设备的当前铃声模式。

接下来,我们添加一个切换条件,在其中更新不同铃声模式的模式文本视图消息。我们还指定了模式文本视图的默认条件文本。

在为模式电视指定文本后,我们为不同的线性布局添加点击监听器

在每个线性布局的点击监听器中,我们通过调用音频管理器来更改音频模式,然后更改当前音频模式的文本。之后,我们只需显示提示消息,告知用户音频模式已更新。

添加上述代码后,只需点击顶部栏中的绿色图标即可在移动设备上运行我们的应用程序。

注意 - 确保您已连接到真实设备或模拟器。

结论

在以上教程中,我们了解了 Android 中的音频管理器是什么,以及如何在 Android 应用程序中使用它来切换不同的音频模式。


相关文章