Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android SoundPool播放音效

android SoundPool播放音效

編輯:關於Android編程

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來管理所加載的聲音

調用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 soundMap =
		new HashMap();
	@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)
	{
		// 判斷哪個按鈕被單擊
		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;
		}
	}
}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved