25、從頭學Android之多媒體--使用SoundPool播放音頻

 

 

SoundPool

構造方法

構造方法

描述

public SoundPool (int maxStreams, int streamType, int srcQuality)

 

參數說明:

maxStreams:指定支持多少個文件

streamType:指定聲音類型

srcQuality:聲音品質

 

常見方法

方法名稱

描述

public int load (Context context, int resId, int priority)

從資源ID所對應的資源加載聲音

public int load (AssetFileDescriptor afd, int priority)

從原始資源文件中加載聲音

public int load (FileDescriptor fd, long offset, long length, int priority)

從原始資源文件中加載聲音並設置加載從哪開始到多長的聲音文件

public int load (String path, int priority)

從指定文件路徑加載聲音

public final void pause (int streamID)

暫停

public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

播放

參數說明:

soundID:資源ID

leftVolume:左頻道聲音

rightVolume:右頻道聲音

loop:-1代表循環,0代表不循環

rate:值0.5-2.0設置1爲正常

public final void release ()

釋放SoundPool對象資源

public final void stop (int streamID)

停止

public void setOnLoadCompleteListener (SoundPool.OnLoadCompleteListener listener)

設置監聽器,在加載音樂文件完成時觸發該事件

 

 

 

 

package com.jiahui.soundpool;

 

import java.util.HashMap;

import java.util.Map;

 

import android.app.Activity;

import android.media.AudioManager;

import android.media.SoundPool;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class SoundPoolDemoActivity extends Activity implements OnClickListener {

 

       private Button btnbomb, btnshot, btnarrow;

 

       private SoundPool soundPool;

       Map<Integer, Integer> soundMap = new HashMap<Integer, Integer>();

 

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.main);

 

              btnbomb = (Button) this.findViewById(R.id.btnbomb);

              btnshot = (Button) this.findViewById(R.id.btnshot);

              btnarrow = (Button) this.findViewById(R.id.btnarrow);

 

              btnbomb.setOnClickListener(this);

              btnshot.setOnClickListener(this);

              btnarrow.setOnClickListener(this);

              // 創建 SoundPool對象設置最多容納10個音頻。音頻的品質爲5

              soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);

 

              // load方法加載音頻文件返回對應的ID

              soundMap.put(1, soundPool.load(this, R.raw.bomb, 1));

              soundMap.put(2, soundPool.load(this, R.raw.shot, 1));

              soundMap.put(3, soundPool.load(this, R.raw.arrow, 1));

 

       }

 

       @Override

       public void onClick(View v) {

              switch (v.getId()) {

              case R.id.btnbomb:

                     soundPool.play(soundMap.get(1), 1, 1, 1, 0, 1);

                     break;

              case R.id.btnshot:

                     soundPool.play(soundMap.get(2), 1, 1, 1, 0, 1);

                     break;

              case R.id.btnarrow:

                     soundPool.play(soundMap.get(3), 1, 1, 1, 0, 1);

                     break;

 

              default:

                     break;

              }

 

       }

}


 

如需轉載引用請註明出處:http://blog.csdn.net/jiahui524

 

 

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