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. iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数

    iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...

  2. C# Read/Write another Process' Memory z

    http://www.codeproject.com/Articles/670373/Csharp-Read-Write-another-Process-Memory This article aim ...

  3. UE 使用技巧

    一.关于正则表达式的使用 删除空行: 替换 %[ ^t]++^p 为 空串 替换回车换行符:替换^p 为 空串 删除行尾空格: 替换 [ ^t]+$ 为 空串 删除行首空格: 替换 %[ ^t]+ 为 ...

  4. HTML5的manifest缓存

    要使用manifest缓存,我们首先需要写一个manifest文件.这个文件有严格的格式要求,下面是个例子CACHE MANIFEST#我是注释,这个文件名叫test.manifestCACHE:/t ...

  5. Centos yum源更新为阿里云

    阿里云镜像网址 http://mirrors.aliyun.com/   更新步骤 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d ...

  6. Windows server 2008 上部署 MVC (NopCommerce 3.4)网站

    自己用开源框架做了个商城,该框架是基于mvc4的,本地编译通过,运行一切正常,关于发布遇到了好几个问题. 本地: IIS7.5. VS2013 总结后发现只需要设置两个问题,就不会有那些古怪的问题:什 ...

  7. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

  8. 实际例子描述和分析“猎豹抢票跨站推荐功能有票刷不到”的疑似bug

    前言 快过年了,又到了一年抢票时.今年douba和douma计划要带着doudou回姥姥家.昨天在家用抢票软件居然发现了一个bug,那就是在猎豹抢票中跨站推荐的车票几天里一直是没有,但是在12306手 ...

  9. URAL-1982 Electrification Plan 最小生成树

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 题意:无向图,给n个点,n^2条边,每条边有个一权值,其中有k个点有发电站,给出这 ...

  10. 【转】nginx的优缺点

    原博文出自于:http://blog.csdn.net/a454211787/article/details/22494485     感谢! 1.nginx相对于apache优点: 轻量级同样起we ...