android中有兩種方式可以播放音頻,一種是SoundPool,一種是MediaPlayer。前者適合短促但對反應速度要求較高的情況(如遊戲中的爆炸聲),後者適合較長當對時間要求不高的情況(如遊戲中的背景音樂)。示例如下:
首先,創建項目Sound,在res下新建目錄raw用於存放聲音文件,講需要的聲音文件放入該目錄。
然後編寫佈局文件main.xml,代碼如下:
最後,在MainActivity.java中編寫代碼,根據不同的按鈕事件執行播放和暫停的操作。代碼如下:
- <?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);
- }
- };
- }