
- Android 基礎
- Android - 主頁
- Android - 概述
- Android - 環境設定
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動
- Android - 服務
- Android - 廣播接收器
- Android - 內容提供器
- Android - 碎片
- Android - 意圖/過濾器
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送電子郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - 音訊管理器
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 攝像頭
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - 谷歌地圖
- Android - 影像效果
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入微調器
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問題與解答
- Android - 實用資源
- Android - 討論
Android - 音訊管理器
您可以輕鬆控制 Android 中的鈴聲音量和鈴聲配置檔案,例如:靜音、振動、響鈴等。Android 提供了 AudioManager 類,可訪問這些控制元件。
為了使用 AndroidManager 類,您必須首先透過呼叫 getSystemService() 方法建立 AudioManager 類的物件。語法如下所示。
private AudioManager myAudioManager; myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
一旦您例項化了 AudioManager 類的物件,就可以使用 setRingerMode 方法設定裝置的音訊或鈴聲配置檔案。語法如下所示。
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
setRingerMode 方法將整數作為引數。對於每種模式,都會分配一個整數,用於區分不同的模式。可能的模式是。
序號 | 模式和描述 |
---|---|
1 |
RINGER_MODE_VIBRATE 此模式將裝置設定為振動模式。 |
2 |
RINGER_MODE_NORMAL 此模式將裝置設定為正常(響亮)模式。 |
3 |
RINGER_MODE_SILENT 此模式將裝置設定為靜音模式。 |
設定模式後,您可以呼叫 getRingerMode() 方法獲取系統的設定狀態。語法如下所示。
int mod = myAudioManager.getRingerMode();
除了 getRingerMode 方法外,AudioManager 類中還有其他方法可用於控制音量和其他模式。它們列在下面。
序號 | 方法和描述 |
---|---|
1 |
adjustVolume(int direction, int flags) 此方法調整最相關的流的音量 |
2 | getMode() 此方法返回當前音訊模式 |
3 | getStreamMaxVolume(int streamType) 此方法返回特定流的最大音量索引 |
4 | getStreamVolume(int streamType) 此方法返回特定流的當前音量索引 |
5 | isMusicActive() 此方法檢查是否有音樂正在播放。 |
6 | startBluetoothSco() 此方法啟動藍牙 SCO 音訊連線 |
7 | stopBluetoothSco() 此方法停止藍牙 SCO 音訊連線。 |
示例
以下示例演示了 AudioManager 類的用法。它建立了一個應用程式,允許您為裝置設定不同的鈴聲模式。
要試驗此示例,您需要在實際裝置上執行它。
步驟 | 描述 |
---|---|
1 | 您將使用 Android Studio IDE 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。 |
2 | 修改 src/MainActivity.java 檔案以新增 AudioManager 程式碼 |
3 | 修改佈局 XML 檔案 res/layout/activity_main.xml,如有必要新增任何 GUI 元件。 |
4 | 修改 res/values/string.xml 檔案並新增必要的字串元件。 |
5 | 修改 AndroidManifest.xml 以新增必要的許可權。 |
6 | 執行應用程式並選擇正在執行的 Android 裝置,將應用程式安裝到裝置上並驗證結果。 |
以下是 src/MainActivity.java 的內容
package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button mode,ring,vibrate,silent; private AudioManager myAudioManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vibrate=(Button)findViewById(R.id.button3); ring=(Button)findViewById(R.id.button2); mode=(Button)findViewById(R.id.button); silent=(Button)findViewById(R.id.button4); myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); vibrate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } }); ring.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); Toast.makeText(MainActivity.this,"Now in Ringing Mode", Toast.LENGTH_LONG).show(); } }); silent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); Toast.makeText(MainActivity.this,"Now in silent Mode", Toast.LENGTH_LONG).show(); } }); mode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int mod=myAudioManager.getRingerMode(); if(mod==AudioManager.RINGER_MODE_VIBRATE){ Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } else if(mod==AudioManager.RINGER_MODE_NORMAL){ Toast.makeText(MainActivity.this,"Now in Ringing Mode", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } } }); } }
以下是 activity_main.xml 的內容
此處 abc 表示 tutorialspoint 的徽標
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Audio Recording" android:id="@+id/textView" android:textSize="30dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorialspoint" android:id="@+id/textView2" android:textColor="#ff3eff0f" android:textSize="35dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView2" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mode" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="59dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ring" android:id="@+id/button2" android:layout_alignTop="@+id/button" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vibrate" android:id="@+id/button3" android:layout_alignTop="@+id/button2" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Silent" android:id="@+id/button4" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" /> </RelativeLayout>
以下是 Strings.xml 的內容
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 的內容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplication" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要在 Android Studio 中執行應用程式,請開啟專案的某個活動檔案,然後單擊工具欄中的執行 圖示。Android Studio 將顯示影像
現在選擇靜音按鈕,您將在通知欄中獲得靜音圖示

現在只需選擇響鈴按鈕,然後按當前模式按鈕檢視其狀態是否已設定。

現在按振動按鈕,然後按當前模式按鈕檢視其是否已設定。它將顯示以下螢幕。
