Android遊戲開發學習筆記(二):音頻的播放

android中有兩種方式可以播放音頻,一種是SoundPool,一種是MediaPlayer。前者適合短促但對反應速度要求較高的情況(如遊戲中的爆炸聲),後者適合較長當對時間要求不高的情況(如遊戲中的背景音樂)。示例如下:

首先,創建項目Sound,在res下新建目錄raw用於存放聲音文件,講需要的聲音文件放入該目錄。

然後編寫佈局文件main.xml,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView   
  8.     android:id="@+id/textView"   
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="沒有播放任何聲音" 
  12.     /> 
  13. <Button 
  14.     android:id="@+id/btn1" 
  15.     android:layout_width="wrap_content" 
  16.     android:layout_height="wrap_content" 
  17.     android:text="使用MediaPlayer播放聲音"   
  18.     /> 
  19. <Button 
  20.     android:id="@+id/btn2" 
  21.     android:layout_width="wrap_content" 
  22.     android:layout_height="wrap_content" 
  23.     android:text="暫停MediaPlayer聲音"   
  24.     /> 
  25. <Button 
  26.     android:id="@+id/btn3" 
  27.     android:layout_width="wrap_content" 
  28.     android:layout_height="wrap_content" 
  29.     android:text="使用SoundPool播放聲音"   
  30.     /> 
  31. <Button 
  32.     android:id="@+id/btn4" 
  33.     android:layout_width="wrap_content" 
  34.     android:layout_height="wrap_content" 
  35.     android:text="暫停SoundPool聲音"   
  36.     /> 
  37. </LinearLayout> 
最後,在MainActivity.java中編寫代碼,根據不同的按鈕事件執行播放和暫停的操作。代碼如下:
  1. package game.test;  
  2.  
  3. import java.util.HashMap;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.media.AudioManager;  
  7. import android.media.MediaPlayer;  
  8. import android.media.SoundPool;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.  
  15. public class MainActivity extends Activity {  
  16.     Button btn1, btn2, btn3, btn4;  
  17.     TextView tv;  
  18.     MediaPlayer media;  
  19.     SoundPool sound;  
  20.     HashMap<Integer, Integer> soundMap;  
  21.  
  22.     /** Called when the activity is first created. */ 
  23.     @Override 
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         initSounds();  
  27.         setContentView(R.layout.main);  
  28.         tv = (TextView) findViewById(R.id.textView);  
  29.         btn1 = (Button) findViewById(R.id.btn1);  
  30.         btn2 = (Button) findViewById(R.id.btn2);  
  31.         btn3 = (Button) findViewById(R.id.btn3);  
  32.         btn4 = (Button) findViewById(R.id.btn4);  
  33.         btn1.setOnClickListener(btn_listener);  
  34.         btn2.setOnClickListener(btn_listener);  
  35.         btn3.setOnClickListener(btn_listener);  
  36.         btn4.setOnClickListener(btn_listener);  
  37.     }  
  38.  
  39.     private void initSounds() {  
  40.         // TODO Auto-generated method stub  
  41.         media = MediaPlayer.create(this, R.raw.shadow);  
  42.         sound = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);  
  43.         soundMap = new HashMap<Integer, Integer>();  
  44.         soundMap.put(1, sound.load(this, R.raw.shake, 1));  
  45.     }  
  46.  
  47.     private OnClickListener btn_listener = new OnClickListener() {  
  48.         @Override 
  49.         public void onClick(View v) {  
  50.             switch (v.getId()) {  
  51.             case R.id.btn1:  
  52.                 tv.setText("使用MediaPlayer播放聲音");  
  53.                 if (!media.isPlaying())  
  54.                     media.start();  
  55.                 break;  
  56.             case R.id.btn2:  
  57.                 tv.setText("暫停了MediaPlayer播放的聲音");  
  58.                 if (media.isPlaying())  
  59.                     media.pause();  
  60.                 break;  
  61.             case R.id.btn3:  
  62.                 tv.setText("使用SoundPool播放聲音");  
  63.                 this.playSound(10);  
  64.                 break;  
  65.             case R.id.btn4:  
  66.                 tv.setText("暫停了SoundPool播放的聲音");  
  67.                 sound.pause(1);  
  68.                 break;  
  69.             }  
  70.         }  
  71.  
  72.         private void playSound(int sound, int loop) {  
  73.             // TODO Auto-generated method stub  
  74.             AudioManager mgr = (AudioManager) MainActivity.this 
  75.                     .getSystemService(Context.AUDIO_SERVICE);  
  76.             float streamVolumeCurrent = mgr  
  77.                     .getStreamVolume(AudioManager.STREAM_MUSIC);  
  78.             float streamVolumeMax = mgr  
  79.                     .getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
  80.             float volume = streamVolumeCurrent / streamVolumeMax;  
  81.             MainActivity.this.sound.play(soundMap.get(sound), volume, volume,  
  82.                     1, loop, 1f);  
  83.         }  
  84.     };  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章