📜  Android中的AudioManager(带示例)

📅  最后修改于: 2021-05-09 16:23:11             🧑  作者: Mango

AudioManager是Android提供的类,可用于控制Android设备的铃声音量。借助此类音频管理器,您可以轻松控制设备的铃声音量。可以通过在Android中调用getSystemService()方法来使用音频管理器类。创建音频管理器类时,可以使用setRingerMode()方法更改设备的铃声音量。 setRingerMode()方法采用整数参数来设置设备的铃声配置文件。 setRingerMode()方法中需要传递三个不同的整数参数,如下所示:

RINGER_MODE_NORMAL This mode will set your device mode to Normal/General mode.
RINGER_MODE_SILENT This mode will set your device mode to Silent mode.
RINGER_MODE_VIBRATE This mode will set your device mode to vibrate mode. 

例子

这是我们创建Ringtone Manager应用程序的简单示例。该应用程序将帮助您将设备的当前状态从常规更改为振动,然后更改为静音模式。下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。注意,我们将使用Java语言实现该项目

Android Sample GIF中的AudioManager

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:在AndroidManifest.xml文件中添加权限

AndroidManifest.xml文件中添加以下行。

步骤3:修改字符串.xml文件

以下是字符串.xml文件的代码。

XML

    GFG Ringtone Manager
    vibrate_mode
    silent_mode
    ringtone_mode
    Welcome to Ringtone Manager App
    Current Mode


XML



      
    
    
      
    
    
  
    
          
        
        
        
        
  
        
  
        
    


Java
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.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // TextView to display the current ringer mode
    TextView currentStateTV;
  
    // Image buttons to switch ringer mode.
    ImageButton silentIB, vibrateIB, ringtoneIB;
  
    // object class variable for audio manager class.
    private AudioManager audioManager;
  
    // current mode to store integer value of ringer mode.
    int currentmode;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        currentStateTV = findViewById(R.id.idTVCurrentMode);
        silentIB = findViewById(R.id.idIBSilentMode);
        vibrateIB = findViewById(R.id.idIBVibrateMode);
        ringtoneIB = findViewById(R.id.idIBRingtoneMode);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  
        // current mode will store current mode
        // of ringer of users device..
        currentmode = audioManager.getRingerMode();
  
        switch (currentmode) {
            case AudioManager.RINGER_MODE_NORMAL:
                currentStateTV.setText("Ringer Mode");
                break;
            case AudioManager.RINGER_MODE_SILENT:
                currentStateTV.setText("Silent Mode");
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                currentStateTV.setText("Vibrate Mode");
            default:
                currentStateTV.setText("Fail to get mode");
        }
  
        silentIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
                // the below code is to check the permission that the access
                // notification policy settings from users device..
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !notificationManager.isNotificationPolicyAccessGranted()) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                    startActivity(intent);
                }
  
                // set ringer mode here will sets your ringer mode to silent mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Toast.makeText(MainActivity.this, "Silent Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Silent Mode Activated..");
            }
        });
  
        vibrateIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set ringer mode here will sets your ringer mode to vibrate mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                Toast.makeText(MainActivity.this, "Vibrate Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Vibrate Mode Activated..");
            }
        });
  
        ringtoneIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set ringer mode here will sets your ringer mode to normal mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                Toast.makeText(MainActivity.this, "Ringtone Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Ringtone Mode Activated..");
            }
        });
    }
}


步骤4:使用activity_main.xml文件

以下是activity_main.xml文件的代码。在代码内部添加了注释,以更详细地了解代码。

XML格式




      
    
    
      
    
    
  
    
          
        
        
        
        
  
        
  
        
    

步骤5:使用MainActivity。 Java文件

下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

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.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // TextView to display the current ringer mode
    TextView currentStateTV;
  
    // Image buttons to switch ringer mode.
    ImageButton silentIB, vibrateIB, ringtoneIB;
  
    // object class variable for audio manager class.
    private AudioManager audioManager;
  
    // current mode to store integer value of ringer mode.
    int currentmode;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        currentStateTV = findViewById(R.id.idTVCurrentMode);
        silentIB = findViewById(R.id.idIBSilentMode);
        vibrateIB = findViewById(R.id.idIBVibrateMode);
        ringtoneIB = findViewById(R.id.idIBRingtoneMode);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  
        // current mode will store current mode
        // of ringer of users device..
        currentmode = audioManager.getRingerMode();
  
        switch (currentmode) {
            case AudioManager.RINGER_MODE_NORMAL:
                currentStateTV.setText("Ringer Mode");
                break;
            case AudioManager.RINGER_MODE_SILENT:
                currentStateTV.setText("Silent Mode");
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                currentStateTV.setText("Vibrate Mode");
            default:
                currentStateTV.setText("Fail to get mode");
        }
  
        silentIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
                // the below code is to check the permission that the access
                // notification policy settings from users device..
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !notificationManager.isNotificationPolicyAccessGranted()) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                    startActivity(intent);
                }
  
                // set ringer mode here will sets your ringer mode to silent mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Toast.makeText(MainActivity.this, "Silent Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Silent Mode Activated..");
            }
        });
  
        vibrateIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set ringer mode here will sets your ringer mode to vibrate mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                Toast.makeText(MainActivity.this, "Vibrate Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Vibrate Mode Activated..");
            }
        });
  
        ringtoneIB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // set ringer mode here will sets your ringer mode to normal mode
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                Toast.makeText(MainActivity.this, "Ringtone Mode Activated..", Toast.LENGTH_SHORT).show();
                currentStateTV.setText("Ringtone Mode Activated..");
            }
        });
    }
}

输出:在您的设备上运行该应用程序

文件夹中使用的Drawable可以在drawable文件夹中看到。以下是GitHub链接以访问该项目的链接:https://github.com/ChaitanyaMunje/GFG-Audio-Manager

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!