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 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的 Run
图标。 Android studio 会显示图片
现在选择静音按钮,您会在通知栏上看到静音图标
现在只需选择响铃按钮,然后按当前模式按钮即可查看其状态是否已设置。
现在按振动按钮,然后按当前模式按钮,看看是否设置。它将显示以下屏幕。

