1. 程式人生 > >SoundPool播放音訊

SoundPool播放音訊

package com.soundpool;

import java.util.HashMap;
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;
import android.annotation.SuppressLint;
import android.app.Activity;


public class MainActivity extends Activity implements OnClickListener{

private Button one,two,three;
private HashMap<Integer, Integer> map;
private SoundPool sp;
@SuppressLint("UseSparseArrays")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

one=(Button) findViewById(R.id.one);
two=(Button) findViewById(R.id.two);
three=(Button) findViewById(R.id.three);

one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
map=new HashMap<Integer, Integer>();
//最大流同步物件 音訊型別(stream_music_常規值) srcquality取樣率轉換質量
sp=new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
map.put(1, sp.load(this, R.raw.bomb, 1));
map.put(2, sp.load(this, R.raw.shot, 1));
map.put(3, sp.load(this, R.raw.arrow, 1));
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
//集合中資料 左聲道範圍值(0-1) 右聲道範圍值(0-1) 優先順序 是否迴圈(1、不迴圈,2、永遠迴圈) 率播放速率(1 =正常播放,範圍0.5至2)
sp.play(map.get(1), 1, 1, 0, 0, 1);
break;
case R.id.two:
sp.play(map.get(1), 1, 1, 0, 0, 1);
break;

case R.id.three:
sp.play(map.get(1), 1, 1, 0, 0, 1);
break;


default:
break;
}
}


}