首页 > 代码库 > Android SoundPool即时音效的使用Demo

Android SoundPool即时音效的使用Demo

当应用程序需要低音频延迟并且(或者)将同时播放多个音频流时(例如播放多种音效和背景音乐的游戏),可以使用SoundPool类来管理音频。

即适合短促且对反应速度比较高的情况(游戏音效或按键声等)。

好了,直接上代码,下面实现了,两个音频文件可同时播放的一个功能(我这里没有短的音频,所以使用的音频资源师两首歌曲的,都只会播放一点点):


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_start_one"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="播放即时音效1" />

    <Button
        android:id="@+id/bt_pause_one"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="暂停即时音效1" />

    <Button
        android:id="@+id/bt_start_two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="播放即时音效2" />

    <Button
        android:id="@+id/bt_pause_two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="暂停即时音效1" />

</LinearLayout>

MainActivity:

package com.android_soundpooldemo;

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;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button bt_start_one;
	private Button bt_start_two;
	private Button bt_pause_one;
	private Button bt_pause_two;
	private SoundPool sp;
	private Map<Integer, Integer> map;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initSoundpool();
		bt_start_one = (Button) findViewById(R.id.bt_start_one);
		bt_start_two = (Button) findViewById(R.id.bt_start_two);
		bt_pause_one = (Button) findViewById(R.id.bt_pause_one);
		bt_pause_two = (Button) findViewById(R.id.bt_pause_two);
		bt_start_one.setOnClickListener(this);
		bt_start_two.setOnClickListener(this);
		bt_pause_one.setOnClickListener(this);
		bt_pause_two.setOnClickListener(this);
	}

	/**
	 * 初始化
	 */
	private void initSoundpool() {
		sp = new SoundPool(5,// 同时播放的音效
				AudioManager.STREAM_MUSIC, 0);
		map = new HashMap<Integer, Integer>();
		map.put(1, sp.load(this, R.raw.good, 1));
		map.put(2, sp.load(this, R.raw.shanghai, 1));
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_start_one:
			playSound(1, 5);// 播放第一首音效,播放一遍
			Toast.makeText(this, "播放第一首音效", 0).show();
			break;
		case R.id.bt_start_two:
			playSound(2, 5);
			Toast.makeText(this, "播放第二首音效", 0).show();
			break;
		case R.id.bt_pause_one:
			sp.pause(map.get(1));
			Toast.makeText(this, "暂停第一首音效", 0).show();
			break;
		case R.id.bt_pause_two:
			sp.pause(map.get(2));
			Toast.makeText(this, "暂停第二首音效", 0).show();
			break;

		default:
			break;
		}

	}

	/**
	 * 
	 * @param sound
	 *            文件
	 * @param number
	 *            循环次数
	 */
	private void playSound(int sound, int number) {
		AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);// 实例化
		float audioMaxVolum = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 音效最大值
		float audioCurrentVolum = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		float audioRatio = audioCurrentVolum / audioMaxVolum;
		sp.play(map.get(sound), audioRatio,// 左声道音量
				audioRatio,// 右声道音量
				1, // 优先级
				number,// 循环播放次数
				1);// 回放速度,该值在0.5-2.0之间 1为正常速度
	}

}

需要源代码的可以到我上传的资源中下载。


Android SoundPool即时音效的使用Demo