編輯:關於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)
使用SoundPool播放聲音的步驟: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對象的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; HashMapsoundMap = 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; } } }
在Android開發時,我們使用的大部分都是Java的api,比如HashMap這個api,使用率非常高,但是對於Android這種對內存非常敏感的移動平台,很多時候使用
本文代碼以MTK平台Android 4.4為分析對象,與Google原生AOSP有些許差異,請讀者知悉。 本文主要介紹MTK Android開機時,SIM卡的Fram
本系列博文我想圍繞在Android中的一些優化細節和大家進行分享。Android中的優化可謂又是一重任,Android不足以像PC端具有很高的內存執行空間給我們用來重量級
CleverCode最近在做微信開發。在調試內網用手機調試微信公眾號開發的時候,發現訪問觸屏版配置host頁面非常麻煩。最好找到一個代理工具Fiddler。1 代理原理1