MediaPlayer與SoundPool的使用場景

轉:http://daikainan.iteye.com/blog/1408015

MediaPlayer:使用簡單,適合做遊戲的背景音樂,資源佔用量較高、延遲時間較長、不支持多個音頻同時播放等。

音樂文件正常播放完畢,而又沒有設置循環播放的話就進入該狀態,並會觸發OnCompletionListener的onCompletion()方法。
此時可以調用start()方法重新從頭播放文件,也可以stop()停止MediaPlayer,或者也可以seekTo()來重新定位播放位置,播放下一首音樂
如果你設置了循環播放  mp.setLooping(true); 的話,那麼永遠都不會監聽到播放完成的狀態!!!!這裏一定要注意!


SoundPool:適合播放遊戲中的特效,如技能聲音,怪物叫聲等時間很短的音樂

1. SoundPool最大隻能申請1M的內存空間。

2. 音頻格式建議使用OGG格式。使用WAV格式的音頻文件存放遊戲音效,經過反覆測試,在音效播放間隔較短的情況下會出現異常關閉的情況

3.在使用SoundPool播放音頻的時候,如果在初始化中就調用播放函數進行播放音樂那麼根本沒有聲音,不是因爲沒有執行,而是 SoundPool需要一準備時間

----------------------------------------------------------------------------------------------------------------
下面以實例說明:

首先看效果

 

 

 

 

 

 

       你不停的點擊在MediaPlayer與SoundPool發射子彈是會發現,SoundPool的更適合做音樂特效,MediaPlayer適合做背景音樂。
     其他不做解釋:自己下載源碼,親自運行,然後打開背景音樂,不停的點擊右側的6個發射按鈕,慢慢體驗,你會明白的。。。

 

 

 

在看代碼

 

Java代碼  收藏代碼
  1. package dk.game;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.pm.ActivityInfo;  
  8. import android.media.AudioManager;  
  9. import android.media.MediaPlayer;  
  10. import android.media.SoundPool;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.view.Window;  
  14. import android.view.WindowManager;  
  15.   
  16. public class MusicTestActivity extends Activity {  
  17.       
  18.     private MediaPlayer mediaPlayerBg;  
  19.     private MediaPlayer mediaPlayerTeXiao1;  
  20.     private MediaPlayer mediaPlayerTeXiao2;  
  21.     private MediaPlayer mediaPlayerTeXiao3;  
  22.     private SoundPool soundPool;  
  23.     private AudioManager am;   
  24.     private int maxVol;  
  25.     private int loadId1,loadId2,loadId3;  
  26.     private Context context;  
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         //全屏顯示  
  31.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  32.         //去掉標題  
  33.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  34.         //橫屏  
  35.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  36.         setContentView(R.layout.main);  
  37.         //獲得當前的上下文  
  38.         context=getApplicationContext();  
  39.           
  40.         mediaPlayerBg=MediaPlayer.create(context, R.raw.bgmusic);  
  41.         //背景音樂循環播放  
  42.         mediaPlayerBg.setLooping(true);  
  43.         mediaPlayerTeXiao1=MediaPlayer.create(context, R.raw.shoot1);  
  44.         mediaPlayerTeXiao2=MediaPlayer.create(context, R.raw.shoot2);  
  45.         mediaPlayerTeXiao3=MediaPlayer.create(context, R.raw.shoot3);  
  46.         //SoundPool的初始化  
  47.         /* 
  48.          * 第一個參數是:同時播放特效的數量 
  49.          * 第二個參數是:音樂播放的音頻流 
  50.          * 第三個參數是:音樂的質量 
  51.          */  
  52.         soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);  
  53.         loadId1=soundPool.load(context, R.raw.shoot1, 1);  
  54.         loadId2=soundPool.load(context, R.raw.shoot2, 1);  
  55.         loadId3=soundPool.load(context, R.raw.shoot3, 1);  
  56.         /* 
  57.          * Android OS中,如果你去按手機上的調節音量的按鈕,會分兩種情況, 
  58.          * 一種是調整手機本身的鈴聲音量,一種是調整遊戲,軟件,音樂播放的音量 
  59.          * 當我們在遊戲中的時候 ,總是想調整遊戲的音量而不是手機的鈴聲音量, 
  60.          * 可是煩人的問題又來了,我在開發中發現,只有遊戲中有聲音在播放的時候 
  61.          * ,你才能去調整遊戲的音量,否則就是手機的音量,有沒有辦法讓手機只要是 
  62.          * 在運行遊戲的狀態就只調整遊戲的音量呢?試試下面這段代碼吧! 
  63.          * setVolumeControlStream(AudioManager.STREAM_MUSIC); 
  64.          * 設定調整音量爲媒體音量,當暫停播放的時候調整音量就不會再默認調整鈴聲音量了 
  65.          */  
  66.         setVolumeControlStream(AudioManager.STREAM_MUSIC);  
  67.           
  68.         // 獲取音頻服務然後強轉成一個音頻管理器,後面方便用來控制音量大小用  
  69.         am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  
  70.         // 獲取最大音量值(15最大! .不是100!)  
  71.         maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
  72.     }  
  73.     /** 
  74.      * 播放背景音樂 
  75.      * @param view 
  76.      */  
  77.     public void startBgMusic(View view){  
  78.         if(mediaPlayerBg != null) {  
  79.               mediaPlayerBg.stop();  
  80.         }  
  81.         try {  
  82.             mediaPlayerBg.prepare();  
  83.         } catch (IllegalStateException e) {  
  84.             e.printStackTrace();  
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         mediaPlayerBg.start();  
  89.     }  
  90.     /** 
  91.      * 停止播放背景音樂 
  92.      * @param view 
  93.      */  
  94.     public void stopBgMusic(View view){  
  95.         mediaPlayerBg.stop();  
  96.     }  
  97.       
  98.       
  99.     /** 
  100.      * MediaPlayer特效1 
  101.      * @param view 
  102.      */  
  103.     public void playTeXiao1(View view){  
  104.         if(mediaPlayerTeXiao1 != null) {  
  105.             mediaPlayerTeXiao1.stop();  
  106.         }  
  107.         try {  
  108.             mediaPlayerTeXiao1.prepare();  
  109.         } catch (IllegalStateException e) {  
  110.             e.printStackTrace();  
  111.         } catch (IOException e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.         mediaPlayerTeXiao1.start();  
  115.     }  
  116.     /** 
  117.      * MediaPlayer特效2 
  118.      * @param view 
  119.      */  
  120.     public void playTeXiao2(View view){  
  121.         if(mediaPlayerTeXiao2 != null) {  
  122.             mediaPlayerTeXiao2.stop();  
  123.         }  
  124.         try {  
  125.             mediaPlayerTeXiao2.prepare();  
  126.         } catch (IllegalStateException e) {  
  127.             e.printStackTrace();  
  128.         } catch (IOException e) {  
  129.             e.printStackTrace();  
  130.         }  
  131.         mediaPlayerTeXiao2.start();  
  132.     }  
  133.     /** 
  134.      * MediaPlayer特效2 
  135.      * @param view 
  136.      */  
  137.     public void playTeXiao3(View view){  
  138.         if(mediaPlayerTeXiao3 != null) {  
  139.             mediaPlayerTeXiao3.stop();  
  140.         }  
  141.         try {  
  142.             mediaPlayerTeXiao3.prepare();  
  143.         } catch (IllegalStateException e) {  
  144.             e.printStackTrace();  
  145.         } catch (IOException e) {  
  146.             e.printStackTrace();  
  147.         }  
  148.         mediaPlayerTeXiao3.start();  
  149.     }  
  150.       
  151.       
  152.     /** 
  153.      * SoundPool特效1 
  154.      * @param view 
  155.      */  
  156.     public void playSoundPoolTeXiao1(View view){  
  157.         //參數1:播放特效加載後的ID值  
  158.         //參數2:左聲道音量大小(range = 0.0 to 1.0)  
  159.         //參數3:右聲道音量大小(range = 0.0 to 1.0)  
  160.         //參數4:特效音樂播放的優先級,因爲可以同時播放多個特效音樂  
  161.         //參數5:是否循環播放,0只播放一次(0 = no loop, -1 = loop forever)  
  162.         //參數6:特效音樂播放的速度,1F爲正常播放,範圍 0.5 到 2.0  
  163.         soundPool.play(loadId1, 0.5f, 0.5f, 10, 1f);  
  164.           
  165.     }  
  166.     /** 
  167.      * SoundPool特效2 
  168.      * @param view 
  169.      */  
  170.     public void playSoundPoolTeXiao2(View view){  
  171.         soundPool.play(loadId2, 0.5f, 0.5f, 10, 1f);  
  172.           
  173.     }  
  174.     /** 
  175.      * SoundPool特效3 
  176.      * @param view 
  177.      */  
  178.     public void playSoundPoolTeXiao3(View view){  
  179.         soundPool.play(loadId3, 0.5f, 0.5f, 10, 1f);  
  180.           
  181.     }  
  182.       
  183. }  

 

 

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/relativeLayout1"  
  9.         android:layout_width="fill_parent"  
  10.         android:background="@drawable/bg"  
  11.         android:layout_height="fill_parent" >  
  12.   
  13.         <Button  
  14.             android:id="@+id/button1"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_alignParentLeft="true"  
  18.             android:layout_alignParentBottom="true"  
  19.             android:text="停止背景音樂"   
  20.             android:onClick="stopBgMusic"  
  21.             />  
  22.          <Button  
  23.             android:id="@+id/button3"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_above="@id/button1"  
  27.             android:layout_alignParentLeft="true"  
  28.             android:text="播放背景音樂"   
  29.             android:onClick="startBgMusic"  
  30.             />  
  31.     
  32.         <Button  
  33.             android:id="@+id/button43"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_alignParentRight="true"  
  37.             android:layout_alignParentBottom="true"  
  38.             android:text="SoundPool發射子彈3"   
  39.             android:onClick="playSoundPoolTeXiao3"  
  40.             />  
  41.         <Button  
  42.             android:id="@+id/button42"  
  43.             android:layout_width="wrap_content"  
  44.             android:layout_height="wrap_content"  
  45.             android:layout_alignParentRight="true"  
  46.             android:layout_above="@id/button43"  
  47.             android:text="SoundPool發射子彈2"   
  48.             android:onClick="playSoundPoolTeXiao2"  
  49.             />  
  50.                   
  51.          <Button  
  52.             android:id="@+id/button41"  
  53.             android:layout_width="wrap_content"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_alignParentRight="true"  
  56.             android:layout_above="@id/button42"  
  57.             android:text="SoundPool發射子彈1"   
  58.             android:onClick="playSoundPoolTeXiao1"  
  59.             />  
  60.            
  61.          <Button  
  62.             android:id="@+id/button23"  
  63.             android:layout_width="wrap_content"  
  64.             android:layout_height="wrap_content"  
  65.             android:layout_toLeftOf="@id/button43"  
  66.             android:layout_alignParentBottom="true"  
  67.             android:text="MediaPlayer發射子彈3"   
  68.             android:onClick="playTeXiao3"  
  69.             />  
  70.            
  71.          <Button  
  72.             android:id="@+id/button22"  
  73.             android:layout_width="wrap_content"  
  74.             android:layout_height="wrap_content"  
  75.            android:layout_toLeftOf="@id/button42"  
  76.             android:layout_above="@id/button23"  
  77.             android:text="MediaPlayer發射子彈2"   
  78.             android:onClick="playTeXiao2"  
  79.             />  
  80.          <Button  
  81.             android:id="@+id/button21"  
  82.             android:layout_width="wrap_content"  
  83.             android:layout_height="wrap_content"  
  84.            android:layout_toLeftOf="@id/button41"  
  85.             android:layout_above="@id/button22"  
  86.             android:text="MediaPlayer發射子彈1"   
  87.             android:onClick="playTeXiao1"  
  88.             />  
  89.            
  90.     </RelativeLayout>  
  91.   
  92. </LinearLayout>  
 
最後附上不錯的音效下載網站:
http://www.2gei.com/
http://sc.chinaz.com/tag_yinxiao/QiangSheng.html

 

最後源碼 :鬱悶文件大小不能超過10MB,分成2個壓縮包了

 

 

 

 

 

 

 

 

 

發佈了28 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章