如何在安卓手机静音模式下播放声音?
介绍
在安卓应用中,我们经常会遇到需要播放通知声音的场景,即使设备处于静音模式也需要播放。本文将介绍如何在安卓手机静音模式下播放声音。
实现
我们将创建一个简单的应用程序,其中包含一个用于显示应用程序标题的文本视图和一个按钮,用于在设备处于静音模式时播放通知声音。
步骤1 - 在Android Studio中创建一个新项目
打开Android Studio,如下图所示。点击“新建项目”创建一个新的Android Studio项目。
点击“新建项目”后,您将看到如下界面。
在此界面中,选择“Empty Activity”,然后点击“Next”。点击“Next”后,您将看到如下界面。
在此界面中,只需指定项目名称即可。包名将自动生成。
注意 - 请确保选择Kotlin作为编程语言。
指定所有详细信息后,点击“Finish”以创建一个新的Android Studio项目。
项目创建完成后,您将看到打开的两个文件:activity_main.xml和MainActivity.java文件。
步骤2:处理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:tools="http://schemas.android.com/tools"
android:id="@+id/idRLLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- text view for displaying heading of the application -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idBtnPlayNotification"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:padding="5dp"
android:text="Play Notification when Android Phone is in Silent Mode"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<!-- on below line we are creating a button to play audio notification -->
<Button
android:id="@+id/idBtnPlayNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:text="Play Notification"
android:textAllCaps="false" />
</RelativeLayout>
说明 - 在上面的代码中,根元素是Android中的相对布局。此布局是一个视图组,用于相对于彼此对齐其中的所有元素。借助ID或位置,我们可以相对地对齐相对布局中的所有元素。
在这个相对布局中,我们首先创建一个文本视图,用于显示应用程序的标题。然后,我们创建一个按钮,用于在设备处于静音模式时播放通知声音。
最后,我们为相对布局添加一个闭合标签,因为文本视图和按钮都包含在我们的相对布局中。
步骤3:处理MainActivity.java
打开MainActivity.java。如果此文件不可见,请在左侧面板中导航到app>java>您的应用包名>MainActivity.java打开此文件。打开此文件后,添加以下代码。代码中添加了注释以便详细了解。
package com.example.androidjavaapp;
import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// on below line creating a variable for button.
private Button playNotificationBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing variables.
playNotificationBtn = findViewById(R.id.idBtnPlayNotification);
// on below line we are adding click listener for button.
playNotificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are creating an intent for send action.
playNotificationSoundInSilentMode();
}
});
}
// on below line creating a method to play notification.
public void playNotificationSoundInSilentMode() {
// on below line creating and initializing variable for audio manager.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// on below line getting original volume.
int originalVolume = am.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
// on below line setting stream volume.
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
// on below line getting notification uri.
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// on below line creating a variable for ringtone and playing it.
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
// on below line calling a handler function to again switch back to silent mode.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// on below line setting stream volumne.
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, originalVolume, 0);
}
}, 500);
}
}
说明 - 在上面的MainActivity.java文件代码中,我们首先为按钮创建一个变量。
现在,我们将看到onCreate方法。这是每个Android应用程序的默认方法。创建应用程序视图时将调用此方法。在此方法中,我们设置内容视图,即名为activity_main.xml的布局文件,以设置来自该文件的UI。
指定视图后,我们初始化按钮的变量。之后,我们为按钮添加了一个点击监听器来播放通知声音。
在onclick方法中,我们调用一个将播放通知声音的方法。在此方法中,我们首先将设备的静音模式更改为常规模式,然后通过调用Uri来播放通知声音。之后,我们再次将设备配置文件切换为静音模式。
添加上述代码后,只需点击顶部的绿色图标即可在移动设备上运行我们的应用程序。
注意 - 请确保已连接到您的真实设备或模拟器。
输出
结论
在以上教程中,我们学习了如何在安卓手机静音模式下播放通知声音。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP