SoundPool
构造方法
构造方法 |
描述 |
public SoundPool (int maxStreams, int streamType, int srcQuality)
|
参数说明: maxStreams:指定支持多少个文件 streamType:指定声音类型 srcQuality:声音品质 |
常见方法
方法名称 |
描述 |
public int load (Context context, int resId, int priority) |
从资源ID所对应的资源加载声音 |
public int load (AssetFileDescriptor afd, int priority) |
从原始资源文件中加载声音 |
public int load (FileDescriptor fd, long offset, long length, int priority) |
从原始资源文件中加载声音并设置加载从哪开始到多长的声音文件 |
public int load (String path, int priority) |
从指定文件路径加载声音 |
public final void pause (int streamID) |
暂停 |
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) |
播放 参数说明: soundID:资源ID leftVolume:左频道声音 rightVolume:右频道声音 loop:-1代表循环,0代表不循环 rate:值0.5-2.0设置1为正常 |
public final void release () |
释放SoundPool对象资源 |
public final void stop (int streamID) |
停止 |
public void setOnLoadCompleteListener (SoundPool.OnLoadCompleteListener listener) |
设置监听器,在加载音乐文件完成时触发该事件 |
package com.jiahui.soundpool; 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; public class SoundPoolDemoActivity extends Activity implements OnClickListener { private Button btnbomb, btnshot, btnarrow; private SoundPool soundPool; Map<Integer, Integer> soundMap = new HashMap<Integer, Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnbomb = (Button) this.findViewById(R.id.btnbomb); btnshot = (Button) this.findViewById(R.id.btnshot); btnarrow = (Button) this.findViewById(R.id.btnarrow); btnbomb.setOnClickListener(this); btnshot.setOnClickListener(this); btnarrow.setOnClickListener(this); // 创建 SoundPool对象设置最多容纳10个音频。音频的品质为5 soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5); // load方法加载音频文件返回对应的ID 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)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnbomb: soundPool.play(soundMap.get(1), 1, 1, 1, 0, 1); break; case R.id.btnshot: soundPool.play(soundMap.get(2), 1, 1, 1, 0, 1); break; case R.id.btnarrow: soundPool.play(soundMap.get(3), 1, 1, 1, 0, 1); break; default: break; } } }
如需转载引用请注明出处:http://blog.csdn.net/jiahui524