1

我下载了一些代码,而不是在本地班级中播放声音,而是调用另一个班级来播放声音,我遇到的唯一问题是它不允许我使用手机上的音量键来调节音量,而我的旧代码在我当地的课堂上允许我这样做。

以旧方式,本地类具有以下代码:

    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) 未定义

该怎么办?谢谢!

4

2 回答 2

5

Actually, if what you want is for the hardware volume keys to adjust the media stream instead of the ringtone stream, you'll want to request that using http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int):

@Override
public void onStart() {
    super.onStart();
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
于 2011-11-08T20:03:18.697 回答
5

如果您想以系统定义的声音级别播放声音,请更改:

mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);

对此:

mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);

您传递到的音量级别play介于 0 和 1 之间。它表示播放声音的系统音量的百分比。因此 0.5 的值将以系统音量的 50% 播放。

于 2011-10-05T18:34:41.267 回答