Android 按钮Button点击音效

2024-05-11 00:12

本文主要是介绍Android 按钮Button点击音效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、新建工程

        编译运行,确保工程无误,这里不过多赘述。

二、UI布局

        添加两个播放音效Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_marginTop="100dp"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="match_parent"android:orientation="vertical"android:layout_margin="20dp"android:layout_height="wrap_content"><Buttonandroid:layout_width="match_parent"android:id="@+id/btn_start1"android:text="start 1"android:layout_height="wrap_content"></Button><Buttonandroid:layout_width="match_parent"android:id="@+id/btn_start2"android:text="start 2"android:layout_height="wrap_content"></Button><Buttonandroid:layout_marginTop="10dp"android:id="@+id/btn_stop"android:text="stop"android:layout_width="match_parent"android:layout_height="wrap_content"></Button></LinearLayout></LinearLayout></LinearLayout>

三、功能实现

Mainactivity.java中添加如下代码

package com.example.sing_test;import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;import java.util.HashMap;public class MainActivity extends AppCompatActivity {private Button btn_start1, btn_start2, btn_stop;SoundPool sp;//声明SoundPool的引用HashMap<Integer, Integer> hm;//声明HashMap来存放声音文件int currStaeamId;//当前正播放的streamId@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/* ------------------------------------------------------------------------------------- */// 隐藏状态栏方法View decorView = getWindow().getDecorView();int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;decorView.setSystemUiVisibility(option);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {getWindow().setStatusBarColor(Color.TRANSPARENT);}// 隐藏标题栏ActionBar actionBar = getSupportActionBar();assert actionBar != null;actionBar.hide();getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//隐藏状态栏但不隐藏状态栏字体//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏,并且不显示字体if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏文字颜色为暗色}/* ------------------------------------------------------------------------------------- */btn_start1 = findViewById(R.id.btn_start1);btn_start2 = findViewById(R.id.btn_start2);btn_stop = findViewById(R.id.btn_stop);initSoundPool();//初始化声音池的方法btn_start1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {playSound(1, 0);//播放1号声音资源,且播放一次//提示播放即时音效Toast.makeText(MainActivity.this, "播放即时音效", Toast.LENGTH_SHORT).show();}});btn_start2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {playSound(2, 0);//播放1号声音资源,且播放一次//提示播放即时音效Toast.makeText(MainActivity.this, "播放即时音效", Toast.LENGTH_SHORT).show();}});btn_stop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {sp.stop(currStaeamId);//停止正在播放的某个声音//提示停止播放Toast.makeText(MainActivity.this, "停止播放即时音效", Toast.LENGTH_SHORT).show();}});}private void playSound(int sound, int loop) {//获取AudioManager引用AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);//获取当前音量float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);//获取系统最大音量float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//计算得到播放音量float volume = streamVolumeCurrent / streamVolumeMax;//调用SoundPool的play方法来播放声音文件currStaeamId = sp.play(hm.get(sound), volume, volume, 1, loop, 1.0f);}private void initSoundPool() {//初始化声音池sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);//创建SoundPool对象hm = new HashMap<Integer, Integer>();//创建HashMap对象//加载声音文件,并且设置为1号声音放入hm中hm.put(1, sp.load(this, R.raw.a, 1));hm.put(2, sp.load(this, R.raw.b, 2));}
}

        res目录下添加raw目录,raw添加所需要的音频文件。

编译运行即可。

结束!

这篇关于Android 按钮Button点击音效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/977962

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(