轉: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個發射按鈕,慢慢體驗,你會明白的。。。
在看代碼
- package dk.game;
- import java.io.IOException;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.ActivityInfo;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.SoundPool;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- public class MusicTestActivity extends Activity {
- private MediaPlayer mediaPlayerBg;
- private MediaPlayer mediaPlayerTeXiao1;
- private MediaPlayer mediaPlayerTeXiao2;
- private MediaPlayer mediaPlayerTeXiao3;
- private SoundPool soundPool;
- private AudioManager am;
- private int maxVol;
- private int loadId1,loadId2,loadId3;
- private Context context;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //全屏顯示
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- //去掉標題
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- //橫屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- setContentView(R.layout.main);
- //獲得當前的上下文
- context=getApplicationContext();
- mediaPlayerBg=MediaPlayer.create(context, R.raw.bgmusic);
- //背景音樂循環播放
- mediaPlayerBg.setLooping(true);
- mediaPlayerTeXiao1=MediaPlayer.create(context, R.raw.shoot1);
- mediaPlayerTeXiao2=MediaPlayer.create(context, R.raw.shoot2);
- mediaPlayerTeXiao3=MediaPlayer.create(context, R.raw.shoot3);
- //SoundPool的初始化
- /*
- * 第一個參數是:同時播放特效的數量
- * 第二個參數是:音樂播放的音頻流
- * 第三個參數是:音樂的質量
- */
- soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
- loadId1=soundPool.load(context, R.raw.shoot1, 1);
- loadId2=soundPool.load(context, R.raw.shoot2, 1);
- loadId3=soundPool.load(context, R.raw.shoot3, 1);
- /*
- * Android OS中,如果你去按手機上的調節音量的按鈕,會分兩種情況,
- * 一種是調整手機本身的鈴聲音量,一種是調整遊戲,軟件,音樂播放的音量
- * 當我們在遊戲中的時候 ,總是想調整遊戲的音量而不是手機的鈴聲音量,
- * 可是煩人的問題又來了,我在開發中發現,只有遊戲中有聲音在播放的時候
- * ,你才能去調整遊戲的音量,否則就是手機的音量,有沒有辦法讓手機只要是
- * 在運行遊戲的狀態就只調整遊戲的音量呢?試試下面這段代碼吧!
- * setVolumeControlStream(AudioManager.STREAM_MUSIC);
- * 設定調整音量爲媒體音量,當暫停播放的時候調整音量就不會再默認調整鈴聲音量了
- */
- setVolumeControlStream(AudioManager.STREAM_MUSIC);
- // 獲取音頻服務然後強轉成一個音頻管理器,後面方便用來控制音量大小用
- am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
- // 獲取最大音量值(15最大! .不是100!)
- maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
- }
- /**
- * 播放背景音樂
- * @param view
- */
- public void startBgMusic(View view){
- if(mediaPlayerBg != null) {
- mediaPlayerBg.stop();
- }
- try {
- mediaPlayerBg.prepare();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- mediaPlayerBg.start();
- }
- /**
- * 停止播放背景音樂
- * @param view
- */
- public void stopBgMusic(View view){
- mediaPlayerBg.stop();
- }
- /**
- * MediaPlayer特效1
- * @param view
- */
- public void playTeXiao1(View view){
- if(mediaPlayerTeXiao1 != null) {
- mediaPlayerTeXiao1.stop();
- }
- try {
- mediaPlayerTeXiao1.prepare();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- mediaPlayerTeXiao1.start();
- }
- /**
- * MediaPlayer特效2
- * @param view
- */
- public void playTeXiao2(View view){
- if(mediaPlayerTeXiao2 != null) {
- mediaPlayerTeXiao2.stop();
- }
- try {
- mediaPlayerTeXiao2.prepare();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- mediaPlayerTeXiao2.start();
- }
- /**
- * MediaPlayer特效2
- * @param view
- */
- public void playTeXiao3(View view){
- if(mediaPlayerTeXiao3 != null) {
- mediaPlayerTeXiao3.stop();
- }
- try {
- mediaPlayerTeXiao3.prepare();
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- mediaPlayerTeXiao3.start();
- }
- /**
- * SoundPool特效1
- * @param view
- */
- public void playSoundPoolTeXiao1(View view){
- //參數1:播放特效加載後的ID值
- //參數2:左聲道音量大小(range = 0.0 to 1.0)
- //參數3:右聲道音量大小(range = 0.0 to 1.0)
- //參數4:特效音樂播放的優先級,因爲可以同時播放多個特效音樂
- //參數5:是否循環播放,0只播放一次(0 = no loop, -1 = loop forever)
- //參數6:特效音樂播放的速度,1F爲正常播放,範圍 0.5 到 2.0
- soundPool.play(loadId1, 0.5f, 0.5f, 1, 0, 1f);
- }
- /**
- * SoundPool特效2
- * @param view
- */
- public void playSoundPoolTeXiao2(View view){
- soundPool.play(loadId2, 0.5f, 0.5f, 1, 0, 1f);
- }
- /**
- * SoundPool特效3
- * @param view
- */
- public void playSoundPoolTeXiao3(View view){
- soundPool.play(loadId3, 0.5f, 0.5f, 1, 0, 1f);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="fill_parent"
- android:background="@drawable/bg"
- android:layout_height="fill_parent" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentBottom="true"
- android:text="停止背景音樂"
- android:onClick="stopBgMusic"
- />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@id/button1"
- android:layout_alignParentLeft="true"
- android:text="播放背景音樂"
- android:onClick="startBgMusic"
- />
- <Button
- android:id="@+id/button43"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:text="SoundPool發射子彈3"
- android:onClick="playSoundPoolTeXiao3"
- />
- <Button
- android:id="@+id/button42"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_above="@id/button43"
- android:text="SoundPool發射子彈2"
- android:onClick="playSoundPoolTeXiao2"
- />
- <Button
- android:id="@+id/button41"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_above="@id/button42"
- android:text="SoundPool發射子彈1"
- android:onClick="playSoundPoolTeXiao1"
- />
- <Button
- android:id="@+id/button23"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/button43"
- android:layout_alignParentBottom="true"
- android:text="MediaPlayer發射子彈3"
- android:onClick="playTeXiao3"
- />
- <Button
- android:id="@+id/button22"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/button42"
- android:layout_above="@id/button23"
- android:text="MediaPlayer發射子彈2"
- android:onClick="playTeXiao2"
- />
- <Button
- android:id="@+id/button21"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/button41"
- android:layout_above="@id/button22"
- android:text="MediaPlayer發射子彈1"
- android:onClick="playTeXiao1"
- />
- </RelativeLayout>
- </LinearLayout>
最後源碼 :鬱悶文件大小不能超過10MB,分成2個壓縮包了