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;
}
}



}

你可能感兴趣的:(android)