3

我可以使用设置 audioTrack 的音量track.setStereoVolume(a,b);

但我找不到获得音量的方法,无论是喜欢getStereoVolume()还是setOnVolumeChangedHandler(...)

如何跟踪音量?

4

2 回答 2

3

You can't get the left and right volume levels of an audio track.

So I propose you create an class representing an AudioTrack.

public class AudioPlayer{
    AudioTrack track;
    float leftVolume;
    float rightVolume;
    public AudioPlayer(){
        //create audio track.
        leftVolume = 1;
        rightVolume = 1;
    }
    public void setStereoVolume(float left, float right){
        this.leftVolume = left;
        this.rightVolume = right;
        track.setStereoVolume(left, right);
    }
}

When you're creating your AudioTrack the stereo volume is at 1.0 for both channels. When the volume is set through AudioPlayer it tracks the new level.

This will work, provided AudioTrack is fully encapsulated within AudioPlayer.

Edit:

An AudioTrack has its own volume level independent of other tracks. The stream the AudioTrackis playing on (STREAM_MUSIC, STREAM_SYSTEM etc.) will also influence the final volume level.

So, say your AudioTrack is at .5,.5 for left and right channels, and its playing on STREAM_MUSIC. If that stream is at .5 volume, the final audio level will be

.5*.5 = .25 //for the left and right channels.

If some automatic volume adjustment happens due to power savings or headphones- whatever, it should happen on the Stream level (or somewhere hidden beyond that.) Still, it shouldn't adjust your AudioTrack volume.

于 2014-03-02T03:29:21.780 回答
1

不是一个真正的答案,只是一个假设:直到现在我还没有测试过它,但这应该可以工作。我认为您不必重写此方法。试试看

 AudioManager audioManager = (AudioManager) 
          getSystemService(AUDIO_SERVICE);

      float volume = (float) audioManager.
          getStreamVolume(AudioManager.STREAM_MUSIC);
于 2013-05-12T09:58:17.580 回答