MainActivity.java
package com.fisnail;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
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;
/**
*
* @author 粪豆的蜗牛(fisnail@163.com)
*
*/
public class MainActivity extends Activity {
/**
* 播放按钮
*/
private Button btn_id_play;
/**
* 停止按钮
*/
private Button btn_id_stop;
/**
* 声明SoundPool引用
*/
private SoundPool soundPool;
/**
* 用于存放声音文件的Map
*/
private HashMap<Integer, Integer> hashMap;
/**
* 当前播放的音频id
*/
private int currentStreamId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_id_play = (Button) findViewById(R.id.btn_main_play);
btn_id_stop = (Button) findViewById(R.id.btn_main_stop);
initSoundPool();
btn_id_play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
playSound(1, 0);
Toast.makeText(getBaseContext(), "开始播放音效",
Toast.LENGTH_SHORT)
.show();
}
});
btn_id_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundPool.stop(currentStreamId);
Toast.makeText(getBaseContext(), "停止播放音效",
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initSoundPool() {
/**
* 创建SoundPool对象 public SoundPool (int maxStreams,
* int streamType, int srcQuality)
* maxStreams:该参数用于设置最多同时能够播放多少个音效
* streamType:该参数设置音频类型,在游戏中通常设置为STREAM_MUSIC
* srcQuality:该参数设置音频文件的质量,目前还不知具体作用,默认设置为0
*/
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
hashMap = new HashMap<Integer, Integer>();
/**
* 加载音频文件 public int load (Context context, int resId,
* int priority)
* context:应用程序上下文 resId:资源id【由于SoundPool设计的初衷是用于无延时的
* 播放游戏中的短促音效,
* 因此实际开发中应该只将长度小于7s的声音资源放进SoundPool,否则可能加载失败或者内存
* 占用过大】 priority:优先级
*/
hashMap.put(1, soundPool.load(this, R.raw.test, 1));
}
private void playSound(int sound, int loop) {
AudioManager audioManager = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
// 获取当前音量
float streamVolumeMax = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// 获取系统最大音量
float volume = streamVolumeCurrent / streamVolumeMax;
// 计算得到播放音量
/**
* 播放音效 public final int play (int soundID, float leftVolume,
* float rightVolume, int priority, int loop, float rate)
* soundID:播放音效的id
* leftVolume:左声道音量 rightVolume:右声道音量 priority:优先级0最低
* loop:循环 0不循环 -1
* 永远循环 rate:播放速度 0.5~2.0f 1.0f为正常速度
*/
currentStreamId = soundPool.play(hashMap.get(sound), volume,
volume, 1,loop, 1.0f);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- Title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_main_title"
android:text="@string/tv_main_title"
android:layout_centerHorizontal="true"
/>
<!--播放按钮 -->
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_main_play"
android:text="@string/btn_main_play_text"
android:layout_below="@id/tv_main_title"/>
<!--停止按钮 -->
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_main_stop"
android:text="@string/btn_main_stop_text"
android:layout_below="@id/btn_main_play"/>
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">即时音效</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<!--MainActivity -->
<!--标题Title -->
<string name="tv_main_title">即时音效</string>
<!--播放按钮 -->
<string name="btn_main_play_text">播放音效</string>
<!--停止按钮 -->
<string name="btn_main_stop_text">停止播放</string>
</resources>