3

I am programming an app to allow for locally stored mp3 file playback as well as streaming from a server.

I use

  AVAudioPlayer *player;
  AVPlayer *player2;

depending on the type of playback. How do I query what the device volume level is initially so that I can set my volume.slider to its initial value?

justdan0227
  • 1,374
  • 18
  • 48

1 Answers1

2

First, import:

#import <AVFoundation/AVAudioSession.h>

Then get The system wide output volume set by the user using:

float volume = [[AVAudioSession sharedInstance] outputVolume];
Hong Duan
  • 4,234
  • 2
  • 27
  • 50
  • Thanks Hong, I thought this was it as well, however every time I call it, the returned value is "1". – justdan0227 Sep 29 '15 at 16:55
  • It's always 1 on simulator, try it on device. – Hong Duan Sep 29 '15 at 17:07
  • So when I do this on a device, the volume is .1235 which is correct. If I then move my slider all the way to the right, the volume gets louder however it I press up on the volume key of the device, it only shows volume at 3 bars. Why is the slider not updating the device volume UI? – justdan0227 Sep 29 '15 at 17:51
  • There are two different volume, one for media, another for ringtone. What you've seen must be the volume of ringtone. – Hong Duan Sep 29 '15 at 17:56
  • how did you change the volume when the slider is moved? – Hong Duan Sep 29 '15 at 18:34
  • Hey Hong, the UI says volume.. as my audio is playing.. and my slider is all the way to the right at "100%" As I press up on the volume of the device, the audio actually get's even louder. Changed with player.volume = myVolumeSlider.value – justdan0227 Sep 30 '15 at 14:33
  • 1
    The property player.volume is to control an audio player’s volume relative to other audio output, not the system volume, that is saying you can have several player and each has its own volume. – Hong Duan Sep 30 '15 at 14:46
  • Interesting, so how do you sync an audio volume slider in an app with the audio on the device shown by pressing the up down on the device? (so my users don't keep telling me its an error?) – justdan0227 Sep 30 '15 at 21:55
  • 1
    Why not use `MPVolumeView`? The system wide output volume can be set directly only by the user; to provide volume control in your app, use the MPVolumeView class. If you have to do it yourself, check this question: http://stackoverflow.com/q/3651252/971070 – Hong Duan Oct 01 '15 at 01:07