码迷,mamicode.com
首页 > 移动开发 > 详细

android SoundPool播放音效

时间:2014-06-09 14:11:22      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   blog   

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播放音效,布布扣,bubuko.com

android SoundPool播放音效

标签:des   android   c   style   class   blog   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3776704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!