Android,在声音池中播放声音
我下载了一些代码,没有在本地类中播放我的声音,而是调用另一个类来播放声音,我唯一的问题是,它不允许我使用手机上的音量键来调整音量,而本地类中的旧代码允许我这样做 按照旧的方式,本地类具有以下代码:Android,在声音池中播放声音,android,soundpool,audio,soundmanager2,Android,Soundpool,Audio,Soundmanager2,我下载了一些代码,没有在本地类中播放我的声音,而是调用另一个类来播放声音,我唯一的问题是,它不允许我使用手机上的音量键来调整音量,而本地类中的旧代码允许我这样做 按照旧的方式,本地类具有以下代码: AudioManager mgr = (AudioManager) GameScreen_bugfix.this.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent = mgr.getStreamVolu
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;
所以我试着这样改变它:
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以红色突出显示,当我将鼠标移到上面时,它会显示:
类型的方法getSystemService(String)未定义
声音管理器
怎么办?
谢谢 如果您想以系统定义的声级播放声音,请更改此选项:
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
为此:
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);
传递给
播放的音量级别限制在0和1之间。它表示播放声音时系统音量的百分比。因此,值.5将以系统音量的50%播放。如果要以系统定义的音量播放声音,请更改以下内容:
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
为此:
mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);
传递给播放的音量级别限制在0和1之间。它表示播放声音时系统音量的百分比。因此,0.5的值将以系统音量的50%播放。实际上,如果您希望硬件音量键调整媒体流而不是铃声流,则需要使用http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int):
实际上,如果您希望硬件音量键调整媒体流而不是铃声流,那么您需要使用http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int):
谢谢我会晚一点再查,如果成功,你就得到了赏金!干杯谢谢我会晚一点再查,如果成功,你就得到了赏金!干杯谢谢仅仅将音量级别更改为1(如已接受的答案所示)是不起作用的。我照你说的做了之后,现在开始工作了。谢谢。仅仅将音量级别更改为1(如已接受的答案所示)是不起作用的。在我照你说的做了之后,它现在起作用了。