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


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值