先贴上代码
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相关推荐

  1. 【Android 应用开发】Android游戏音效实现

    1. 游戏音效SoundPool 游戏中会根据不同的动作 , 产生各种音效 , 这些音效的特点是短暂(叫声,爆炸声可能持续不到一秒) , 重复(一个文件不断重复播放) , 并且同时播放(比如打怪时怪的 ...

  2. Android多媒体之SoundPool+pcm流的音频操作

    零.前言 今天比较简单,先理一下录制和播放的四位大将 再说一下SoundPool的使用和pcm转wav 讲一下C++文件如何在Android中使用,也就是传说中的JNI 最后讲一下变速播放和变调播放 ...

  3. Android 即时通讯开发小结(二)

    <Android 即时通讯开发小结>基于IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对 IM 开发做一个全面的总结. 相关推荐阅读:. Android即时通讯 ...

  4. Android 即时通讯开发小结(一)

    本文将基于 IM Andriod 开发的各种常见问题,结合网易云信即时通讯技术的实践,对 IM 开发做一个全面的总结. 客户端架构 作为一个 IM 软件,最重要的一个特性就是保证消息的达到率和实时性. ...

  5. Android 即时通讯开发

    Android 即时连天工具的开发 客户端,开三个线程,thread1监听接收消息,用udp,thread2定时向服务器发送在线消息,用tcp,thread3每隔一段时间检查发送的消息的时间是否超时( ...

  6. Android即时聊天系统设计

    Android即时聊天系统设计 最近比较闲,当工作比较闲是意味着什么呢(⊙﹏⊙) 于是,总结一下前几个月的毕业设计吧,说不好对一些有需要的朋友有帮助呢,也算是为社会做贡献了( 简单的即时聊天系统,设计 ...

  7. openfire android 发送图片,基于openfire+smack开发Android即时聊天应用[四]-单人聊天、群聊、发送接收文件等...

    这篇文章主要介绍如何实现点对点单人聊天.多人的群聊.以及如何给对方发送文件,如何发送图片消息和语音消息等功能. 1.单人聊天 1.首先创建聊天对象 /** * 创建聊天窗口 * @param jid ...

  8. 基于openfire+smack开发Android即时聊天应用[三]-账号信息、添加好友、JID理解等

    基于openfire+smack开发Android即时聊天应用[三]-账号信息.添加好友.JID理解等 标签: SmackOpenfireandroid 2015-10-30 18:06  3068人 ...

  9. Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)

    需要源码请点赞关注收藏后评论区留下QQ~~~ 一.铃声播放 虽然媒体播放器MediaPlayer既可用来播放视频,也可以用来播放音频,但是在具体的使用场合,MediaPlayer存在某些播音方面的不足 ...

最新文章

  1. 替代TabActivity,底部菜单主框架搭建
  2. SimNIBS一款无创脑刺激仿真软件安装
  3. docker 安装centos7并SSH远程连接
  4. Python 中文分词(结巴分词)
  5. 2017年10月07日普及组 单元格
  6. PDH-SDH光端机指示灯具体含义介绍
  7. 精益或六西格玛已经不易了,为啥还需要“精益六西格玛”?
  8. 高质量的工程代码为什么难写
  9. HDU 5972 Regular Number
  10. android随机数方法,Android生成随机数的方法实例
  11. 移动端适配之二:visual viewport、layout viewport和ideal viewport介绍 1
  12. 【机器学习】广义回归神经网络(GRNN)的python实现
  13. C语言考试题及答案(一)
  14. java 实现手机号码(String)校验----两种方法
  15. OSPF——基本概念2(多区域与Vlink)
  16. 5G网络入门基础--5G网络的实现流程
  17. Linux编写简单的脚本
  18. 最新推荐 | 清华NLP图神经网络GNN论文分门别类,16大应用200+篇论文
  19. springboot +ldap认证
  20. day01 -云计算简介与华为云计算解决方案

热门文章

  1. mysql group by 去除重复_mysql中distinct和group by过滤删除重复行
  2. Excel如何为单元格内的部分文字添加超链接
  3. 【笔记】css实现文字横向排列/竖向排列
  4. 基于单片机红外遥控LED控制灯亮灭设计毕设课设资料
  5. 修改群名时服务器忙请重试,iOS修改群名称
  6. DDR,DDR2,DDR3,SDRAM比较区别
  7. 宽带隙器件增强电机控制设计
  8. C语言入门part5
  9. 如何做好网站安全防护 防止网站被黑?
  10. 使用FFmpeg 批量处理视频