歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中SoundPool的使用

Android中SoundPool的使用

日期:2017/3/1 10:08:55   编辑:Linux編程

在Android開發中我們經常使用MediaPlayer來播放音頻文件,但是MediaPlayer存在一些不足,例如:資源占用量較高、延遲時間較長、不支持多個音頻同時播放等。這些缺點決定了MediaPlayer在某些場合的使用情況不會很理想,例如在對時間精准度要求相對較高的游戲開發中。

在游戲開發中我們經常需要播放一些游戲音效(比如:子彈爆炸,物體撞擊等),這些音效的共同特點是短促、密集、延遲程度小。在這樣的場景下,我們可以使用SoundPool代替MediaPlayer來播放這些音效。

SoundPool的使用步驟是 :

1.在res中新建raw文件夾,然後將需要播放的音頻放入其中;

2.初始化SoundPool實例;

3.調用SoundPool的play函數進行播放。

幾個重要的函數:

soundPool的構造函數

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

參數:
maxStreams 同時最大播放音頻個數
streamType 音頻流的類型,通常使用 STREAM_MUSIC.
srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.

音效播放函數:
public void playSounds(int sound, int number){
//實例化AudioManager對象,控制聲音
AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
//最大音量
float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//當前音量
float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
//播放
sp.play(spMap.get(sound), //聲音資源
volumnRatio, //左聲道
volumnRatio, //右聲道
1, //優先級,0最低
number, //循環次數,0是不循環,-1是永遠循環
1); //回放速度,0.5-2.0之間。1為正常速度
}

代碼清單

主Activity:

  1. package com.example.soundpool;
  2. import java.util.HashMap;
  3. import android.media.AudioManager;
  4. import android.media.SoundPool;
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. public class SPActivity extends Activity {
  12. private Button playBtn;
  13. private Button pauseBtn;
  14. private SoundPool sp;
  15. private HashMap<Integer,Integer> spMap;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_sp);
  20. playBtn=(Button)findViewById(R.id.button1);
  21. pauseBtn=(Button)findViewById(R.id.button2);
  22. sp=new SoundPool(2,AudioManager.STREAM_MUSIC,0);
  23. spMap = new HashMap<Integer,Integer>();
  24. spMap.put(1, sp.load(this, R.raw.sound1, 1));
  25. playBtn.setOnClickListener(new OnClickListener() {
  26. public void onClick(View v) {
  27. // TODO Auto-generated method stub
  28. playSounds(1,1);
  29. }
  30. });
  31. pauseBtn.setOnClickListener(new OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. // TODO Auto-generated method stub
  35. sp.pause(spMap.get(1));
  36. }
  37. });
  38. }
  39. @Override
  40. public boolean onCreateOptionsMenu(Menu menu) {
  41. getMenuInflater().inflate(R.menu.activity_sp, menu);
  42. return true;
  43. }
  44. public void playSounds(int sound, int number){
  45. AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
  46. float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  47. float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
  48. float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
  49. sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1);
  50. }
  51. }

布局代碼:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <Button
  6. android:id="@+id/button1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_alignLeft="@+id/textView1"
  10. android:layout_below="@+id/textView1"
  11. android:layout_marginTop="42dp"
  12. android:text="Play" />
  13. <Button
  14. android:id="@+id/button2"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignBaseline="@+id/button1"
  18. android:layout_alignBottom="@+id/button1"
  19. android:layout_centerHorizontal="true"
  20. android:text="Pouse" />
  21. </RelativeLayout>

遇到的一個小問題:

pause按鈕在第一次播放的時候好用,第一次之後的播放就不管用了,網上找到的解釋:

原來這個流對應的ID是需要play方法返回的,後來我用mPresentPlayId存儲play返回的流ID,在stop時將流ID使用mPresentPlayId來替換就沒問題了,後來輸出了下mPresentPlayId的值,發現這個值第一次是2.第二次是4,以後使用這個方法一定要注意這個問題。

Copyright © Linux教程網 All Rights Reserved