Android平台中关于音频的播放有两种方式,一种是SoundPool,一种是MediaPlayer。SoundPool适合短促但对反应速度要求高的情况,而MediaPlayer则适合较长但对时间要求不高的情况。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="没有播放任何声音" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用MediaPlayer播放声音" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停MediaPlayer声音" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用SoundPool播放声音" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停SoundPool声音" />
</LinearLayout>
Java代码:
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button btn1, btn2, btn3, btn4;
TextView textView;
MediaPlayer mediaPlayer;
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initSounds();
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
textView = (TextView) findViewById(R.id.textView1);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}
@SuppressLint("UseSparseArrays")
public void initSounds() {
mediaPlayer = MediaPlayer.create(this, R.raw.backsound);// 初始化mediaplayer
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
}
public void playSound(int sound, int loop) {
AudioManager mgr = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr
.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
// 播放声音
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
}
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
// 单击MediaPlayer播放声音
textView.setText("使用MediaPlayer播放声音");
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.button2:
textView.setText("暂停MediaPlayer播放的声音");
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.button3:
textView.setText("使用SoundPool播放声音");
this.playSound(1, 0);
break;
case R.id.button4:
textView.setText("暂停SoundPool播放的声音");
soundPool.pause(1);
break;
default:
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (null != mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
if (null != soundPool) {
soundPool.release();
soundPool = null;
}
}
}