布局文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ch_2013_4_9playsound"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ch_2013_4_9playsound.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
Activity:
package com.example.ch_2013_4_9playsound; import java.util.HashMap; import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; @SuppressLint("UseSparseArrays")
public class MainActivity extends Activity implements OnClickListener { Button btn_play;
Button btn_play1;
Button btn_stop;
Button btn_stop1; SoundPool sp;
HashMap<Integer, Integer> spMap; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); InitSound();
Init();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//http://www.cnblogs.com/sosoft/ public void Init() {
btn_play = (Button) findViewById(R.id.btn_play);
btn_play1 = (Button) findViewById(R.id.btn_play1);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_stop1 = (Button) findViewById(R.id.btn_stop1); btn_play.setOnClickListener(this);
btn_play1.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_stop1.setOnClickListener(this);
} public void InitSound() {
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
spMap = new HashMap<Integer, Integer>();
spMap.put(1, sp.load(this, R.raw.fire, 1));
spMap.put(2, sp.load(this, R.raw.hit, 1)); } public void playSound(int sound, int number) {
AudioManager am = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = volumnCurrent / audioMaxVolumn; sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1f);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btn_play) {
playSound(1, 0);
Toast.makeText(this, "play1", Toast.LENGTH_SHORT).show();
}
if (v == btn_play1) {
playSound(2, 1);
Toast.makeText(this, "play2", Toast.LENGTH_SHORT).show();
}
if (v == btn_stop) {
sp.pause(spMap.get(1));
Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show();
}
if (v == btn_stop1) {
sp.pause(spMap.get(2));
Toast.makeText(this, "stop1", Toast.LENGTH_SHORT).show();
}
} }