MediaPlayer的缺点:

资源占用量高,延时时间较长

不支持多个音效同一时候播放

SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音量,播放比率等參数,避免使用SoundPool来播放歌曲或者做游戏背景音乐,仅仅有那些短促的密集的声音才考虑使用SoundPool播放

构造器:

public SoundPool (int maxStreams, int streamType, int srcQuality)

Parameters

maxStreams the maximum number of simultaneous streams for this SoundPool object

streamType the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.

srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.

Returns

a SoundPool object, or null if creation failed

载入声音:

public int load (AssetFileDescriptor afd, int priority)

Parameters

afd an asset file descriptor

priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

public int load (Context context, int resId, int priority)

Parameters

context the application context

resId the resource ID

priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

public int load (String path, int priority)

Parameters

path the path to the audio file

priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

public int load (FileDescriptor fd, long offset, long length, int priority)

Parameters

fd a FileDescriptor object

offset offset to the start of the sound

length length of the sound

priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.

播放声音:

public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

Parameters

soundID a soundID returned by the load() function

leftVolume left volume value (range = 0.0 to 1.0)

rightVolume right volume value (range = 0.0 to 1.0)

priority stream priority (0 = lowest priority)

loop loop mode (0 = no loop, -1 = loop forever)

rate playback rate (1.0 = normal playback, range 0.5 to 2.0)

使用SoundPool播放声音的步骤:

调用SoundPool的构造器创建SoundPool的对象

调用SoundPool对象的load()方法从指定资源,文件,中载入声音,最好使用HashMap<Integer,Integer>来管理所载入的声音

调用SoundPool的play方法播放声音

样例程序:

import java.util.HashMap;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class SoundPoolTest extends Activity implements OnClickListener
{
Button bomb, shot, arrow;
// 定义一个SoundPool
SoundPool soundPool;
HashMap<Integer, Integer> soundMap =
new HashMap<Integer, Integer>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bomb = (Button) findViewById(R.id.bomb);
shot = (Button) findViewById(R.id.shot);
arrow = (Button) findViewById(R.id.arrow);
// 设置最多可容纳10个音频流,音频的品质为5
soundPool = new SoundPool(10
, AudioManager.STREAM_SYSTEM, 5); //①
// load方法载入指定音频文件,并返回所载入的音频ID。
// 此处使用HashMap来管理这些音频流
soundMap.put(1, soundPool.load(this, R.raw.bomb, 1)); //②
soundMap.put(2, soundPool.load(this, R.raw.shot, 1));
soundMap.put(3, soundPool.load(this, R.raw.arrow, 1));
bomb.setOnClickListener(this);
shot.setOnClickListener(this);
arrow.setOnClickListener(this);
} // 重写OnClickListener监听器接口的方法
@Override
public void onClick(View source)
{
// 推断哪个button被单击
switch (source.getId())
{
case R.id.bomb:
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1); //③
break;
case R.id.shot:
soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1);
break;
case R.id.arrow:
soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1);
break;
}
}
}

android SoundPool播放音效的更多相关文章

  1. 使用SoundPool播放音效

    针对应用程序经常需要播放密集.短促的音效,因为MediaPlayer存在如下缺点: 1.资源占用量较高.延迟时间较长. 2.不支持多个音效同时播放. SoundPool使用音效池的概念来管理多个短促的 ...

  2. Android 使用SoundPool播放音效

    在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...

  3. Android soundpool初探

    内容:本编播客主要讲解一下“即时音效”: 特点:快,短. 在播放这类时间短但是要求反应迅速的的音效,就不能够用不能够使用播放时间较长的音乐播放技术了,而应该采取soundpool技术来播放. soun ...

  4. cocoslua3.17 android机器上播放音效不全

    开发过程中遇到一个问题,一个8秒的音效,在android机器上播放不完就结束了:网上说是由于android播放音效的内存限制的:原因知道了,那怎么解决呢? 通过各种搜索查找发现还是解决不了问题,然后自 ...

  5. Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题

    最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.c ...

  6. Android SoundPool.play方法的音量与系统音量的关系

    Android中播放音频,除了使用MediaPlayer,还可以使用SoundPool.SoundPool使用音效池的概念来管理多个短促的音效,对于需要短促的提示音的场景特别合适. 通常调用Sound ...

  7. Android中播放音乐的几种方式

    前言 前几天一直在研究RxJava2,也写了记录了几篇博客,但因为工作任务原因,需要研究音频相关的知识,暂时放下Rxjava,本文的demo中,MediaPalyer 部分使用RxJava编写一点逻辑 ...

  8. 【Android】播放音频的几种方式介绍

    接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...

  9. 【竞品分析】Android音乐播放器的竞品分析

    迄今为止最长的一篇博客,各位看官笑纳~~ 本次分析基于Android平台,选取了几款我体验过的播放器进行比较分析.主要分为两类,一类是大而全的,功能全面,可满足用户管理歌曲.导入导出歌单等多方面需求, ...

随机推荐

  1. pl/sql developer 连接本地ORACLE 11g 64位数据库

    1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ...

  2. 硬盘结构介绍--mbr及分区

    硬盘刚买来使用时需要经过分区然后格式化才能够使用,硬盘经过分区后,分区软件便会写一个主引导扇区,这个扇区位于硬盘的 0 磁道 0 柱面第1扇区(即0区)(注意:该扇区为隐含扇区,0道0面的全部扇区均为 ...

  3. 【转】错误日志ID8021来源BROWSER导致电脑死机

    现场工控机死机,网上查了篇文章,具体原因还有待分析,下面是图 在这里有必要介绍两个ID号:6006和6005.在事件查看器里ID号为6006的事件表示事件日志服务已停止,如果你没有在当天的事件查看器中 ...

  4. HDU5758 Explorer Bo 树形dp

    我是参考这一篇写的:http://blog.csdn.net/fsss_7/article/details/52049474 一点感想:dp[i][0]代表以这个点为根的且总叶子数为偶数个叶子的答案 ...

  5. IOC框架Ninject实践总结

    原文地址:http://www.cnblogs.com/jeffwongishandsome/archive/2012/04/15/2450462.html IOC框架Ninject实践总结 一.控制 ...

  6. Javascript手记-执行环境和作用域

    执行环境是javascript一个重要的概念,执行环境定义了变量有权访问其他数据决定了他们各自的行为,每个执行环境 都有一个与之关联的变量,环境中定义的所有变量和函数都保存在这个对象中,虽然我们编写的 ...

  7. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  8. maven 的各种命令

    mvn clean : 清理旧的文件 mvn clean compile :  清理 .编译 mvn clean test :       清理 .编译 .测试 mvn clean package : ...

  9. http和数据库sql分析与窃听技术

    用tunnel,tunnel是一种技术称谓,将其放到真正的服务器和客户端之间.调试阶段可以使用webcream运行tomcat作为模拟的真正的服务器. 具体:用apache axis及其项目中的工具t ...

  10. Gym 100507D Zhenya moves from the dormitory (模拟)

    Zhenya moves from the dormitory 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Descrip ...