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个压缩包了

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值