android游戏开发系列(1)——迅雷不及掩耳的声音
这种声音是短而快的声音,应该采用android.media.SoundPool实现。
SoundPool的特点:
1. SoundPool载入音乐文件使用了独立的线程,不会阻塞UI主线程的操作。但是这里如果音效文件过大没有载入完成,我们调用play方法时可能产生严重的后果,这里Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解媒体文件是否载入完成,我们重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可获得。
2. 从上面的onLoadComplete方法可以看出该类有很多参数,比如类似id,是的SoundPool在load时可以处理多个媒体一次初始化并放入内存中,这里效率比MediaPlayer高了很多。
3. SoundPool类支持同时播放多个音效,这对于游戏来说是十分必要的,而MediaPlayer类是同步执行的只能一个文件一个文件的播放。
使用方法:
1. 创建一个SoundPool
public SoundPool(int maxStream, int streamType, int srcQuality)
maxStream —— 同时播放的流的最大数量
streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)
srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值
eg.
SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
创建了一个最多支持3个流同时播放的,类型标记为音乐的SoundPool。
2 一般把多个声音放到HashMap中去,比如
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
soundpool的加载:
int load(Context context, int resId, int priority) //从APK资源载入
int load(FileDescriptor fd, long offset, long length, int priority) //从FileDescriptor对象载入
int load(AssetFileDescriptor afd, int priority) //从Asset对象载入
int load(String path, int priority) //从完整文件路径名载入
最后一个参数为优先级。
3 播放
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ,其中leftVolume和rightVolume表示左右音量,priority表示优先级,loop表示循环次数,rate表示速率,如
//速率最低0.5最高为2,1代表正常速度
sp.play(soundId, 1, 1, 0, 0, 1);
而停止则可以使用 pause(int streamID) 方法,这里的streamID和soundID均在构造SoundPool类的第一个参数中指明了总数量,而id从0开始。
例子:
package wyf.zcl; 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.widget.Button;
import android.widget.Toast; public class MyActivity extends Activity {
/** Called when the activity is first created. */
SoundPool sp; //得到一个声音池引用
HashMap<Integer,Integer> spMap; //得到一个map的引用
Button b1; //声音播放控制按钮
Button b1Pause; //声音暂停控制按钮
Button b2; //声音播放控制按钮
Button b2Pause; //声音暂停控制按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSoundPool(); //初始化声音池
b1=(Button)findViewById(R.id.Button01);//声音播放控制按钮实例化
b2=(Button)findViewById(R.id.Button02);//声音播放控制按钮实例化
b1Pause=(Button)findViewById(R.id.Button1Pause);
b2Pause=(Button)findViewById(R.id.Button2Pause);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(1,1); //播放第一首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show();
}});
b1Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(1));
Toast.makeText(MyActivity.this, "暂停音效1", Toast.LENGTH_SHORT).show();
}});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(2,1); //播放第二首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show();
}});
b2Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(2));
Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show();
}});
}
public void initSoundPool(){ //初始化声音池
sp=new SoundPool(
5, //maxStreams参数,该参数为设置同时能够播放多少音效
AudioManager.STREAM_MUSIC, //streamType参数,该参数设置音频类型,在游戏中通常设置为:STREAM_MUSIC
0 //srcQuality参数,该参数设置音频文件的质量,目前还没有效果,设置为0为默认值。
);
spMap=new HashMap<Integer,Integer>();
spMap.put(1, sp.load(this, R.raw.attack02, 1));
spMap.put(2, sp.load(this, R.raw.attack14, 1));
}
public void playSound(int sound,int number){ //播放声音,参数sound是播放音效的id,参数number是播放音效的次数
AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);//实例化AudioManager对象
float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //返回当前AudioManager对象的最大音量值
float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//返回当前AudioManager对象的音量值
float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
sp.play(
spMap.get(sound), //播放的音乐id
volumnRatio, //左声道音量
volumnRatio, //右声道音量
1, //优先级,0为最低
number, //循环次数,0无不循环,-1无永远循环
1 //回放速度 ,该值在0.5-2.0之间,1为正常速度
);
}
}
代码下载:http://download.csdn.net/detail/lxq_xsyu/6268039
运行结果:
android游戏开发系列(1)——迅雷不及掩耳的声音的更多相关文章
- android游戏开发系列(2)——背景音乐播放技术
背景音乐通常播放时间较长,且文件体积也相对较大.这类资源如果放在内存中,一方面给硬件资源本身就很紧缺的手机造成了负担,另一方面通常也没有这方面的需求,放在内存中,在调用时播放速度较快,而长时音乐文件通 ...
- Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...
- Android游戏开发实践(1)之NDK与JNI开发03
Android游戏开发实践(1)之NDK与JNI开发03 前面已经分享了两篇有关Android平台NDK与JNI开发相关的内容.以下列举前面两篇的链接地址,感兴趣的可以再回顾下.那么,这篇继续这个小专 ...
- Android游戏开发实践(1)之NDK与JNI开发01
Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...
- Android游戏开发实践(1)之NDK与JNI开发02
Android游戏开发实践(1)之NDK与JNI开发02 承接上篇Android游戏开发实践(1)之NDK与JNI开发01分享完JNI的基础和简要开发流程之后,再来分享下在Android环境下的JNI ...
- Android 快速开发系列 打造万能的ListView GridView 适配器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写 ...
- Android游戏开发实践(1)之NDK与JNI开发04
Android游戏开发实践(1)之NDK与JNI开发04 有了前面几篇NDK与JNI开发相关基础做铺垫,再来通过代码说明下这方面具体的操作以及一些重要的细节.那么,就继续NDK与JNI的学习总结. 作 ...
- [Android游戏开发]八款开源 Android 游戏引擎 (巨好的资源)
初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道该从何处入手,每当遇到自己无法解决的难题时,又往往会一边羡慕于 iPhone下有诸如Cocos2d-iphone之类的免费游戏引 ...
- Android游戏开发之旅 View类详解
Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...
随机推荐
- C语言深度剖析-----指针数组和数组指针的分析
指针数组和数组指针的分析 数组类型 定义数组类型 数组指针 这个不可能为数组指针,指向数组首元素 例 指针数组 例 main函数的参数 例 小结
- 四种卸载Mac软件的方法
从 Mac 电脑上卸载已经安装的应用程序可能是你知道的操作系统里面最简单的一种了.而如果你是一名新买了 Mac 电脑的用户,那么你可能比较困惑:怎么没有控制面板中的相应板块来卸载它们呢?但是其实你想不 ...
- 【习题5-1 UVA - 1593】Alignment of Code
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题,每一列都选最长的那个字符串,然后后面加一个空格就好. 这个作为场宽. 模拟输出就好. [代码] #include <b ...
- 3D 应用程序性能
原文:3D 应用程序性能 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/74595999 3 ...
- MFC切换图片防止闪烁
处理WM_ERASEBKGND消息,在消息处理函数中return TRUE;
- Spring Cloud底层原理
目录 一.业务场景介绍 二.Spring Cloud核心组件:Eureka 三.Spring Cloud核心组件:Feign 四.Spring Cloud核心组件:Ribbon 五.Spring Cl ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- java数组10大技巧
0. 声明一个数组(Declare an array) String[] aArray = new String[5]; String[] bArray = {"a"," ...
- [Angular] Testing @Input and @Output bindings
Component: import { Component, Input, ChangeDetectionStrategy, EventEmitter, Output } from '@angular ...
- 利用函数的惰性载入提高 javascript 代码性能
在 javascript 代码中,因为各浏览器之间的行为的差异,我们经常会在函数中包含了大量的 if 语句,以检查浏览器特性,解决不同浏览器的兼容问题.例如,我们最常见的为 dom 节点添加事件的函数 ...