android SoundPool播放音效
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播放音效的更多相关文章
- 使用SoundPool播放音效
针对应用程序经常需要播放密集.短促的音效,因为MediaPlayer存在如下缺点: 1.资源占用量较高.延迟时间较长. 2.不支持多个音效同时播放. SoundPool使用音效池的概念来管理多个短促的 ...
- Android 使用SoundPool播放音效
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高.延迟时间较长.不支持多个音频同时播放等.这些缺点决定了MediaP ...
- Android soundpool初探
内容:本编播客主要讲解一下“即时音效”: 特点:快,短. 在播放这类时间短但是要求反应迅速的的音效,就不能够用不能够使用播放时间较长的音乐播放技术了,而应该采取soundpool技术来播放. soun ...
- cocoslua3.17 android机器上播放音效不全
开发过程中遇到一个问题,一个8秒的音效,在android机器上播放不完就结束了:网上说是由于android播放音效的内存限制的:原因知道了,那怎么解决呢? 通过各种搜索查找发现还是解决不了问题,然后自 ...
- Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题
最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.c ...
- Android SoundPool.play方法的音量与系统音量的关系
Android中播放音频,除了使用MediaPlayer,还可以使用SoundPool.SoundPool使用音效池的概念来管理多个短促的音效,对于需要短促的提示音的场景特别合适. 通常调用Sound ...
- Android中播放音乐的几种方式
前言 前几天一直在研究RxJava2,也写了记录了几篇博客,但因为工作任务原因,需要研究音频相关的知识,暂时放下Rxjava,本文的demo中,MediaPalyer 部分使用RxJava编写一点逻辑 ...
- 【Android】播放音频的几种方式介绍
接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...
- 【竞品分析】Android音乐播放器的竞品分析
迄今为止最长的一篇博客,各位看官笑纳~~ 本次分析基于Android平台,选取了几款我体验过的播放器进行比较分析.主要分为两类,一类是大而全的,功能全面,可满足用户管理歌曲.导入导出歌单等多方面需求, ...
随机推荐
- operator.itemgetter的用法【转】
operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [,, ...
- Delphi DecodeDate和EncodeDate 拆分和聚合时间函数的用法
SysUtilsprocedure DecodeData(Date: TDateTime; var Year, Month, Day: Word);DecodeDate打断TdateTime成为年月日 ...
- 开源GIS简介
原文 开源GIS C++开源GIS中间件类库: GDAL(栅格)/OGR(矢量)提供了类型丰富的读写支持 GEOS(Geometry Engine Open Source)是基于C++的空间拓扑分析实 ...
- [转] C#中发送消息给指定的窗口,以及接收消息
原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...
- 用duilib制作仿QQ2013动态背景登录器
转载请说明原出处,谢谢~~ 在上一篇博客里,我修复了CActiveXUI控件的bug,从而可以使用flash动画来制作程序的背景,这篇博客说明一下应该怎么使用CActiveXUI控件创建透明无窗体的背 ...
- bzoj 3732 Network(最短路+倍增 | LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...
- sublime text 2使用经验
1. Package Control 安装代码: import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.instal ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- Mongoose:Schema之路
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. Mongoose学习 这里的Mongoose当然不是图片上的萌物,它是一个MongoDB对象建模工具(object model ...
- Android 不同应用通过SharedPreference实现共享数据
Android不同应用之间数据的共享有许多方式,但是我觉得还是使用sharedPreference比较简单和轻量级.如果程序B想要访问程序A的sharedPreference可以通过下面的语句来实现: ...