Android 中的音訊管理器


什麼是 Android 中的音訊管理器?

音訊管理器顧名思義,它用於管理 Android 應用程式中的音訊配置檔案。這通常用於在 Android 裝置的不同音訊模式之間切換,例如鈴聲模式、靜音模式和振動模式。

音訊管理器的實現

我們將建立一個簡單的應用程式,其中我們將簡單地顯示 3 個按鈕和 2 個文字檢視。Android 應用程式中的 3 個按鈕用於在不同的音訊模式之間切換,例如鈴聲模式、靜音模式和振動模式。我們將在文字檢視中顯示當前啟用的模式,以通知使用者當前在使用者裝置中激活了哪種模式。我們將遵循分步指南在 Android 應用程式中實現吐司訊息。

步驟 1:在 Android Studio 中建立新專案

導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立新的 Android Studio 專案。

單擊“新建專案”後,您將看到下面的螢幕。

在此螢幕中,我們只需選擇“空活動”並單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。

在此螢幕中,我們只需指定專案名稱。然後包名將自動生成。

注意 - 確保將語言選擇為 Kotlin。

指定所有詳細資訊後,單擊“完成”以建立新的 Android Studio 專案。

建立專案後,我們將看到開啟的 2 個檔案,即 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 system service 並傳遞我們要使用的服務名稱來初始化我們的音訊管理器變數。我們將在應用程式中使用音訊服務來管理音訊模式。然後,我們初始化我們的 current mode 變數,並在其中設定裝置的當前鈴聲模式。

然後,我們在其中添加了一個 switch 條件,用於更新不同鈴聲模式的 mode text view 訊息。我們還為 mode text view 指定了預設條件文字。

為 modeTV 指定文字後,我們為不同的線性佈局新增點選監聽器

在每個線性佈局的 onClick 監聽器中,我們透過呼叫音訊管理器然後更改當前音訊模式的文字來更改音訊模式。之後,我們只是顯示吐司訊息以告知使用者音訊模式已更新。

新增上述程式碼後,現在我們只需單擊頂部的綠色圖示即可在移動裝置上執行我們的應用程式。

注意 - 確保已連線到您的真實裝置或模擬器。

結論

在上面的教程中,我們學習了什麼是 Android 中的音訊管理器以及如何在 Android 應用程式中使用它在 Android 應用程式的不同音訊模式之間切換。

更新時間: 2023 年 3 月 14 日

613 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告