[英]Android, playing sound in soundpool
我下載了一些代碼,而不是在本地課程中播放聲音,而是調用另一個課程來播放聲音,我唯一的問題是在我以前的代碼播放的同時,不允許我使用手機上的音量鍵來調節音量在我當地的課堂上允許我這樣做。
舊的方法是,本地類具有以下代碼:
AudioManager mgr = (AudioManager) GameScreen_bugfix.this.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0,1f);
新代碼如下所示:
public static void playSound(int index,float speed)
{
float streamVolume;
try {
streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
} catch (Exception e) {
e.printStackTrace();
//Log.v("THE ERROR", mSoundPoolMap+" "+index);
}
}
所以我試圖這樣改變它:
AudioManager mgr = (AudioManager) SoundManager.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr
.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
但是,此getSystemService用紅色突出顯示,當我將鼠標懸停在上面時,它說:
對於SoundManager類型,未定義方法getSystemService(String)
該怎么辦? 謝謝!
如果要以系統定義的聲音級別播放聲音,請更改以下內容:
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
對此:
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);
傳遞給您play
的音量級別介於0到1之間。它代表播放聲音的系統音量的百分比。 因此,.5值將占系統音量的50%。
實際上,如果您要讓硬件音量鍵調整媒體流而不是鈴聲流,則需要使用http://developer.android.com/reference/android/app/Activity.html進行請求#setVolumeControlStream(int):
@Override
public void onStart() {
super.onStart();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.