Android 简明教程
Android - Audio Manager
您可以在 Android 中轻松控制铃声音量和铃声配置文件,例如:(静音、振动、响铃等)。Android 提供 AudioManager 类,提供对这些控件的访问。
You can easily control your ringer volume and ringer profile i-e:(silent,vibrate,loud e.t.c) in android. Android provides AudioManager class that provides access to these controls.
要使用 AndroidManager 类,您必须首先通过调用 getSystemService() 方法创建 AudioManager 类的对象。其语法如下。
In order to use AndroidManager class, you have to first create an object of AudioManager class by calling the getSystemService() method. Its syntax is given below.
private AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
实例化 AudioManager 类的对象后,您可以使用 setRingerMode 方法设置设备的音频或铃声配置文件。其语法如下。
Once you instantiate the object of AudioManager class, you can use setRingerMode method to set the audio or ringer profile of your device. Its syntax is given below.
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
setRingerMode 方法接受一个整数作为参数。对于每种模式,都会分配一个整数,以区分不同模式。可能的模式有:
The method setRingerMode takes an integer number as a parameter. For each mode , an integer number is assigned that will differentiate between different modes. The possible modes are.
Sr.No |
Mode & Description |
1 |
RINGER_MODE_VIBRATE This Mode sets the device at vibrate mode. |
2 |
RINGER_MODE_NORMAL This Mode sets the device at normal(loud) mode. |
3 |
RINGER_MODE_SILENT This Mode sets the device at silent mode. |
一旦设置了模式,您就可以调用 getRingerMode() 方法来获取系统的设置状态。其语法如下。
Once you have set the mode , you can call the getRingerMode() method to get the set state of the system. Its syntax is given below.
int mod = myAudioManager.getRingerMode();
除了 getRingerMode 方法外,AudioManager 类中还有其他方法可用于控制音量和其他模式。它们如下所示。
Apart from the getRingerMode method, there are other methods available in the AudioManager class to control the volume and other modes. They are listed below.
Sr.No |
Method & description |
1 |
adjustVolume(int direction, int flags) This method adjusts the volume of the most relevant stream |
2 |
getMode() This method returns the current audio mode |
3 |
getStreamMaxVolume(int streamType) This method returns the maximum volume index for a particular stream |
4 |
getStreamVolume(int streamType) This method returns the current volume index for a particular stream |
5 |
isMusicActive() This method checks whether any music is active. |
6 |
startBluetoothSco() This method Start bluetooth SCO audio connection |
7 |
stopBluetoothSco() This method stop bluetooth SCO audio connection. |
Example
以下示例演示了 AudioManager 类使用方法。 它创建一个应用程序,允许您为您的设备设置不同的铃声模式。
The below example demonstrates the use of AudioManager class. It creates a application that allows you to set different ringer modes for your device.
要尝试此示例,您需要在实际设备上运行此示例。
To experiment with this example , you need to run this on an actual device.
Steps |
Description |
1 |
You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 |
Modify src/MainActivity.java file to add AudioManager code |
3 |
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 |
Modify res/values/string.xml file and add necessary string components. |
5 |
Modify AndroidManifest.xml to add necessary permissions. |
6 |
Run the application and choose a running android device and install the application on it and verify the results. |
以下是 src/MainActivity.java 的内容
Here is the content of 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 的内容。
Here is the content of activity_main.xml
<?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 的内容
Here is the content of Strings.xml
<resources>
<string name="app_name">My Application</string>
</resources>
以下是 AndroidManifest.xml 的内容
Here is the content of 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 将显示图像
Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android Studio, open one of your project’s activity files and click Run icon from the tool bar. Android studio will display Images
现在选择静音按钮,您将在通知栏中获取静音图标
Now select silent button, you would get silent icon at Notification bar
现在只需选择响铃按钮,然后按当前模式按钮查看其状态是否已设置。
Now just select the ring button and then press the current mode button to see that if its status has been set.
现在按下振动按钮,然后按下当前模式按钮,查看是否已设置。它会显示以下屏幕。
Now press the Vibrate button and then press the current mode button to see that if it is set or not.It will display the following screen.