Android 即时音效SoundPool
先贴上代码
package com.example.voice;import java.util.HashMap;
import java.util.Map;import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button bt_start_one;private Button bt_start_two;private Button bt_pause_one;private Button bt_pause_two;private SoundPool sp;private Map<Integer, Integer> map;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initSoundpool();bt_start_one = (Button) findViewById(R.id.bt_start_one);bt_start_two = (Button) findViewById(R.id.bt_start_two);bt_pause_one = (Button) findViewById(R.id.bt_pause_one);bt_pause_two = (Button) findViewById(R.id.bt_pause_two);bt_start_one.setOnClickListener(this);bt_start_two.setOnClickListener(this);bt_pause_one.setOnClickListener(this);bt_pause_two.setOnClickListener(this);}/*** 初始化*/private void initSoundpool() {sp = new SoundPool(5,// 同时播放的音效AudioManager.STREAM_MUSIC, 0);map = new HashMap<Integer, Integer>();map.put(1, sp.load(this, R.raw.attack02, 1));map.put(2, sp.load(this, R.raw.attack14, 1));}public void onClick(View v) {switch (v.getId()) {case R.id.bt_start_one:playSound(1, 10);// 播放第一首音效,播放一遍Toast.makeText(this, "播放第一首音效", 0).show();break;case R.id.bt_start_two:playSound(2, 10);Toast.makeText(this, "播放第二首音效", 0).show();break;case R.id.bt_pause_one:sp.pause(map.get(1));Toast.makeText(this, "暂停第一首音效", 0).show();break;case R.id.bt_pause_two:sp.pause(map.get(2));Toast.makeText(this, "暂停第二首音效", 0).show();break;default:break;}}/*** * @param sound 文件* @param number 循环次数*/private void playSound(int sound, int number) {AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);// 实例化float audioMaxVolum = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 音效最大值float audioCurrentVolum = am.getStreamVolume(AudioManager.STREAM_MUSIC);float audioRatio = audioCurrentVolum / audioMaxVolum;sp.play(map.get(sound), audioRatio,// 左声道音量audioRatio,// 右声道音量1, // 优先级number,// 循环播放次数1);// 回放速度,该值在0.5-2.0之间 1为正常速度}}
界面代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Button android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/bt_start_one"android:layout_marginBottom="10dip"android:text="播放即时音效1"/><Button android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/bt_pause_one"android:layout_marginBottom="10dip"android:text="暂停即时音效1"/><Button android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/bt_start_two"android:layout_marginBottom="10dip"android:text="播放即时音效2"/><Button android:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/bt_pause_two"android:layout_marginBottom="10dip"android:text="暂停即时音效1"/>
</LinearLayout>
Android 即时音效SoundPool相关推荐
- 【Android 应用开发】Android游戏音效实现
1. 游戏音效SoundPool 游戏中会根据不同的动作 , 产生各种音效 , 这些音效的特点是短暂(叫声,爆炸声可能持续不到一秒) , 重复(一个文件不断重复播放) , 并且同时播放(比如打怪时怪的 ...
- Android多媒体之SoundPool+pcm流的音频操作
零.前言 今天比较简单,先理一下录制和播放的四位大将 再说一下SoundPool的使用和pcm转wav 讲一下C++文件如何在Android中使用,也就是传说中的JNI 最后讲一下变速播放和变调播放 ...
- Android 即时通讯开发小结(二)
<Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对 IM 开发做一个全面的总结. 相关推荐阅读:. Android即时通讯 ...
- Android 即时通讯开发小结(一)
本文将基于 IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对 IM 开发做一个全面的总结. 客户端架构 作为一个 IM 软件,最重要的一个特性就是保证消息的达到率和实时性. ...
- Android 即时通讯开发
Android 即时连天工具的开发 客户端,开三个线程,thread1监听接收消息,用udp,thread2定时向服务器发送在线消息,用tcp,thread3每隔一段时间检查发送的消息的时间是否超时( ...
- Android即时聊天系统设计
Android即时聊天系统设计 最近比较闲,当工作比较闲是意味着什么呢(⊙﹏⊙) 于是,总结一下前几个月的毕业设计吧,说不好对一些有需要的朋友有帮助呢,也算是为社会做贡献了( 简单的即时聊天系统,设计 ...
- openfire android 发送图片,基于openfire+smack开发Android即时聊天应用[四]-单人聊天、群聊、发送接收文件等...
这篇文章主要介绍如何实现点对点单人聊天.多人的群聊.以及如何给对方发送文件,如何发送图片消息和语音消息等功能. 1.单人聊天 1.首先创建聊天对象 /** * 创建聊天窗口 * @param jid ...
- 基于openfire+smack开发Android即时聊天应用[三]-账号信息、添加好友、JID理解等
基于openfire+smack开发Android即时聊天应用[三]-账号信息.添加好友.JID理解等 标签: SmackOpenfireandroid 2015-10-30 18:06 3068人 ...
- Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
需要源码请点赞关注收藏后评论区留下QQ~~~ 一.铃声播放 虽然媒体播放器MediaPlayer既可用来播放视频,也可以用来播放音频,但是在具体的使用场合,MediaPlayer存在某些播音方面的不足 ...
最新文章
- 替代TabActivity,底部菜单主框架搭建
- SimNIBS一款无创脑刺激仿真软件安装
- docker 安装centos7并SSH远程连接
- Python 中文分词(结巴分词)
- 2017年10月07日普及组 单元格
- PDH-SDH光端机指示灯具体含义介绍
- 精益或六西格玛已经不易了,为啥还需要“精益六西格玛”?
- 高质量的工程代码为什么难写
- HDU 5972 Regular Number
- android随机数方法,Android生成随机数的方法实例
- 移动端适配之二:visual viewport、layout viewport和ideal viewport介绍 1
- 【机器学习】广义回归神经网络(GRNN)的python实现
- C语言考试题及答案(一)
- java 实现手机号码(String)校验----两种方法
- OSPF——基本概念2(多区域与Vlink)
- 5G网络入门基础--5G网络的实现流程
- Linux编写简单的脚本
- 最新推荐 | 清华NLP图神经网络GNN论文分门别类,16大应用200+篇论文
- springboot +ldap认证
- day01 -云计算简介与华为云计算解决方案