3

I can set volume of audioTrack by using track.setStereoVolume(a,b);

But i couldn't find a method to to get the volume, either like getStereoVolume() or setOnVolumeChangedHandler(...)

How can i track the volume level?

frankish
  • 6,738
  • 9
  • 49
  • 100
  • I think using AudioManager could be a solution. There is a method getStreamVolume(int streamType); – Opiatefuchs May 10 '13 at 14:29
  • I think it is a general overrider function. So even AudioTrack's volume is 0.0f, AudioManager may return 1.0f for getStreamVolume. Isn't it? – frankish May 10 '13 at 14:32

2 Answers2

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.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • Thank you for your answer. If track volume is changed by Android OS (for example like auto volume change on Samsung devices when you plug-in headphones), these values will be out of sync. – frankish Mar 02 '14 at 11:11
  • I don't think so. There are multiple volume levels. Your audio track will have its volume level independently of any other level. The final level will be multiplied by the stream type its outputting on. I'll elaborate in answer. – William Morrison Mar 02 '14 at 19:41
1

Not a real answer, just an assumption: I have not tested it until now, but this should work. I don´t think that You have to override this method. Try it

 AudioManager audioManager = (AudioManager) 
          getSystemService(AUDIO_SERVICE);

      float volume = (float) audioManager.
          getStreamVolume(AudioManager.STREAM_MUSIC);
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • 3
    The problem is i am decreasing volume of only 1 audiotrack. So another audiotrack may have a volume of 1.0 (full). So getStreamvolume on AudioManager may be set at 1.0 or 0.5 independent of these values. – frankish May 12 '13 at 10:01