Android中SoundPool的使用
在遊戲開發中我們經常需要播放一些遊戲音效(比如:子彈爆炸,物體撞擊等),這些音效的共同特點是短促、密集、延遲程度小。在這樣的場景下,我們可以使用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:
package com.example.soundpool;
import java.util.HashMap;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SPActivity extends Activity {
private Button playBtn;
private Button pauseBtn;
private SoundPool sp;
private HashMap<Integer,Integer> spMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);
playBtn=(Button)findViewById(R.id.button1);
pauseBtn=(Button)findViewById(R.id.button2);
sp=new SoundPool(2,AudioManager.STREAM_MUSIC,0);
spMap = new HashMap<Integer,Integer>();
spMap.put(1, sp.load(this, R.raw.sound1, 1));
playBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
playSounds(1,1);
}
});
pauseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp.pause(spMap.get(1));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sp, menu);
return true;
}
public void playSounds(int sound, int number){
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, number, 1);
}
}
佈局程式碼:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="Play" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Pouse" />
</RelativeLayout>
遇到的一個小問題:
pause按鈕在第一次播放的時候好用,第一次之後的播放就不管用了,網上找到的解釋:
原來這個流對應的ID是需要play方法返回的,後來我用mPresentPlayId儲存play返回的流ID,在stop時將流ID使用mPresentPlayId來替換就沒問題了,後來輸出了下mPresentPlayId的值,發現這個值第一次是2.第二次是4,以後使用這個方法一定要注意這個問題。
相關文章
- android 使用 SoundPool 語音播報Android
- Android soundpool release卡住Android
- android 音訊播放 SoundPoolAndroid音訊
- Android中用SoundPool播放音訊Android音訊
- Android多媒體之SoundPool+pcm流的音訊操作Android音訊
- Android中SearchView的使用AndroidView
- Android中的廣播使用Android
- Cordova在Android中的使用Android
- Android中Handler的正確使用Android
- android 中感測器的使用Android
- Android中Lottie的簡單使用Android
- Android中WebView的使用指南:AndroidWebView
- Android中Retrofit的封裝使用Android封裝
- Android中的LruCache的原理和使用Android
- Android開發教程-使用DataBinding(六)RecyclerViewAdapter中的使用AndroidViewAPT
- 【譯】Dagger2在Android中的使用Android
- 【筆記】【Android】Manifest中的intent-filter使用筆記AndroidIntentFilter
- Android開發教程 - 使用Data Binding(三)在Activity中的使用Android
- Android開發教程 - 使用Data Binding(四)在Fragment中的使用AndroidFragment
- Android Compose 的使用Android
- Android Studio中的外掛ButterKnife的配置及其使用方法Android
- Android應用開發中如何使用隱藏的APIAndroidAPI
- Android中的NotificationAndroid
- Android中的VectorAndroid
- Android中的targetsdkversioinAndroid
- android中的ListViewAndroidView
- Android中的RecyclerViewAndroidView
- Android中的intentAndroidIntent
- android中的ViewPageAndroidView
- Android中的AOPAndroid
- Android中的BitmapAndroid
- Android中的ClassLoaderAndroid
- Android 中的 WindowAndroid
- Android解析ClassLoader(二)Android中的ClassLoaderAndroid
- Android進階(七)Android中的ClassLoaderAndroid
- Android之adb的使用Android
- Android中RecyclerView與Scrollview組合使用(二)AndroidView
- Android Studio中如何支援使用Lambda表示式Android
- Android中的Web ServiceAndroidWeb