android SoundPool例子,Android SoundPool即时音效的使用Demo
当应用程序需要低音频延迟并且(或者)将同时播放多个音频流时(例如播放多种音效和背景音乐的游戏),可以使用SoundPool类来管理音频。
即适合短促且对反应速度比较高的情况(游戏音效或按键声等)。
好了,直接上代码,下面实现了,两个音频文件可同时播放的一个功能(我这里没有短的音频,所以使用的音频资源师两首歌曲的,都只会播放一点点):
布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/bt_start_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="播放即时音效1" />
android:id="@+id/bt_pause_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="暂停即时音效1" />
android:id="@+id/bt_start_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="播放即时音效2" />
android:id="@+id/bt_pause_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="暂停即时音效1" />
MainActivity:
package com.android_soundpooldemo;
import java.util.HashMap;
import java.util.Map;
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;
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 map;
@Override
public 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();
map.put(1, sp.load(this, R.raw.good, 1));
map.put(2, sp.load(this, R.raw.shanghai, 1));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start_one:
playSound(1, 5);// 播放第一首音效,播放一遍
Toast.makeText(this, "播放第一首音效", 0).show();
break;
case R.id.bt_start_two:
playSound(2, 5);
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为正常速度
}
} 需要源代码的可以到我上传的资源中下载。
android SoundPool例子,Android SoundPool即时音效的使用Demo相关推荐
- android 开关按钮例子,Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法...
1.ToggleButton属性: 1>有两种状态:选中和未选中状态并需要为不同的状态设置不同的显示文本 2>android:checked="true" 3>a ...
- android webview 例子,android – webview shouldinterceptrequest示例
那么简短的答案是它的工作原理与shouldOverrideUrlLoading(WebView视图,String url)非常相似,如 WebView tutorial所示. 要开始使用,请参阅下面的 ...
- android api 设置,Android API中文文档EditText
Android API中文文档EditText是本文要介绍的内容,主要是来了解并学习Android API文档的帮助,具体关于Android API的内容的详解来看本文. 一.Android API中 ...
- Android 即时音效SoundPool
先贴上代码 package com.example.voice;import java.util.HashMap; import java.util.Map;import android.media. ...
- android 声音池,Android使用SoundPool实现播放音效
如果在程序应用中(比如:游戏的音效等)需要播放密集.短促的音效,这时就使用SoundPool来播放音效,SoundPool使用音效池的概念来管理多个短促的音效,例如它可以开始就10个音效,以后在程序中 ...
- android 播放声音资源,android播放音效例子 (翻页音效、警报音效通用 只需传入声音源)...
音效播放: 资源文件: res/raw/filename 声音池类:SoundPool sp = new SoundPool(同时最大播放个数,AudioManager.STREAM_MUSIC,0) ...
- android 播放提示音,[转载]android播放音效例子 (翻页音效、警报音效通用
音效播放: 资源文件: res/raw/filename 声音池类:SoundPool sp = new SoundPool(同时最大播放个数,AudioManager.STREAM_MUSIC,0) ...
- Android 多媒体开发 MediaPlayer|SoundPool |MediaRecorder |Camera2| MediaProjectionManager
android支持音频格式: mp3.wav.3gp 视频: MP4.g3p 等等.... 1. MediaPlayer|SoundPool 使用 1.1. MediaPlayer 加载资源 ...
- Android中声音池SoundPool的使用
对于MediaPlayer无法同时播放多个声音的问题,Android提供了声音池工具SoundPool,使用声音池即可对多个声音的播放进度调度. 1.SoundPool优点 资源占用量小,不像Medi ...
- Android 音频播放之SoundPool的使用和封装(自定义声音资源)
例如,现在在一个五子棋游戏中,我们需要在棋子落盘的时候播放一段声音.我们可以利用SoundPool,因为它时间很短,而且需要反复播放,并且我们不希望声音占用太大资源. 一般大家使用的是MediaPla ...
最新文章
- 官方教育网代理地址MSN
- mysql 可重复读 快照_MYSQL可重复读及原理、快照读和当前读
- ajax 参数大小限制,Ajax中的POST数据大小是否有限制?
- java cssselector类_CSS 元素选择器
- win10版本查看_想知道电脑中安装的win10版本号,用这3招就对了,一键查看
- 数据库索引为什么会失效
- WPF中自定义MarkupExtension
- 前端安全系列之二:如何防止CSRF攻击?
- 计算机情绪识别属于什么方向,情绪检测器
- android 获取经纬度的三种方法,Android获取经纬度
- 互联网赚钱骚操作,真的太骚了
- 【百元钱买百只鸡问题】一只公鸡5元钱,一只母鸡3元钱,三只小鸡1元钱。要求100元买100只鸡,请给出所有可行的结果?
- 视频转换成gif (知乎)
- PPT精品模板:个人创意销售企业公司介绍PPT
- 给定两点确定一条直线
- java nio 与io_Java NIO 与IO区别
- 计算机应用技术第二版笔记,(计算机应用技术笔记.doc
- 链接服务器——获取EXCEL数据
- 45.Cookie、Session、ServletContext数据共享
- 基于大数据背景下的全国各大城市地铁客流量分析