Rya*_*yan 1 audio android soundpool soundmanager2
我下载了一些代码,而不是在我的本地课堂上播放我的声音,调用另一个类来播放声音,我唯一的问题是它不允许我在手机上使用我的音量键调整音量而我的旧代码在我的本地课上允许我这样做.
以旧的方式,本地类有这个代码:
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);
Run Code Online (Sandbox Code Playgroud)
新代码如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试着改变它:
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;
Run Code Online (Sandbox Code Playgroud)
但是这个getSystemService以红色突出显示,当我鼠标悬停时,它说:
对于SoundManager类型,方法getSystemService(String)未定义
该怎么办?谢谢!
如果您想在系统定义的声级播放声音,请更改:
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
Run Code Online (Sandbox Code Playgroud)
对此:
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);
Run Code Online (Sandbox Code Playgroud)
传递给的音量级别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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3824 次 |
| 最近记录: |