android SoundPool播放音效

來源:互聯網
上載者:User

標籤:android   des   style   blog   class   code   

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;// 定義一個SoundPoolSoundPool soundPool;HashMap<Integer, Integer> soundMap =new HashMap<Integer, Integer>();@Overridepublic 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個音頻流,音訊品質為5soundPool = 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監聽器介面的方法@Overridepublic 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;}}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.