android中有两种方式可以播放音频,一种是SoundPool,一种是MediaPlayer。前者适合短促但对反应速度要求较高的情况(如游戏中的爆炸声),后者适合较长当对时间要求不高的情况(如游戏中的背景音乐)。示例如下:
首先,创建项目Sound,在res下新建目录raw用于存放声音文件,讲需要的声音文件放入该目录。
然后编写布局文件main.xml,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/textView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="没有播放任何声音"
- />
- <Button
- android:id="@+id/btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="使用MediaPlayer播放声音"
- />
- <Button
- android:id="@+id/btn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="暂停MediaPlayer声音"
- />
- <Button
- android:id="@+id/btn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="使用SoundPool播放声音"
- />
- <Button
- android:id="@+id/btn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="暂停SoundPool声音"
- />
- </LinearLayout>
- package game.test;
- import java.util.HashMap;
- 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.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- Button btn1, btn2, btn3, btn4;
- TextView tv;
- MediaPlayer media;
- SoundPool sound;
- HashMap<Integer, Integer> soundMap;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initSounds();
- setContentView(R.layout.main);
- tv = (TextView) findViewById(R.id.textView);
- btn1 = (Button) findViewById(R.id.btn1);
- btn2 = (Button) findViewById(R.id.btn2);
- btn3 = (Button) findViewById(R.id.btn3);
- btn4 = (Button) findViewById(R.id.btn4);
- btn1.setOnClickListener(btn_listener);
- btn2.setOnClickListener(btn_listener);
- btn3.setOnClickListener(btn_listener);
- btn4.setOnClickListener(btn_listener);
- }
- private void initSounds() {
- // TODO Auto-generated method stub
- media = MediaPlayer.create(this, R.raw.shadow);
- sound = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
- soundMap = new HashMap<Integer, Integer>();
- soundMap.put(1, sound.load(this, R.raw.shake, 1));
- }
- private OnClickListener btn_listener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn1:
- tv.setText("使用MediaPlayer播放声音");
- if (!media.isPlaying())
- media.start();
- break;
- case R.id.btn2:
- tv.setText("暂停了MediaPlayer播放的声音");
- if (media.isPlaying())
- media.pause();
- break;
- case R.id.btn3:
- tv.setText("使用SoundPool播放声音");
- this.playSound(1, 0);
- break;
- case R.id.btn4:
- tv.setText("暂停了SoundPool播放的声音");
- sound.pause(1);
- break;
- }
- }
- private void playSound(int sound, int loop) {
- // TODO Auto-generated method stub
- AudioManager mgr = (AudioManager) MainActivity.this
- .getSystemService(Context.AUDIO_SERVICE);
- float streamVolumeCurrent = mgr
- .getStreamVolume(AudioManager.STREAM_MUSIC);
- float streamVolumeMax = mgr
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- float volume = streamVolumeCurrent / streamVolumeMax;
- MainActivity.this.sound.play(soundMap.get(sound), volume, volume,
- 1, loop, 1f);
- }
- };
- }