0

I have a radio streaming AVPlayer and I created a slider which controls the volume for the player.

-(void)adjustVolume
{
    if (self.player != nil)
    {
        NSLog(@"Adjusting volume...");
        self.player.volume = volumeControl.value;
         NSLog(@"%f",self.player.volume);
    }
}

I need to connect the value from the slider to the values I have when pushing the volume buttons. So for example if I move the slider to 50% and after that I push the volume up button on the side of the phone, I need it to increase from 50%, as that was the volume I've set through the slider.

How can I do this?

LATER EDIT:

With this code I managed to update the slider as i press the volume keys, but I can't drag the slider anymore since it always updates with the system volume.

audioSession = [AVAudioSession sharedInstance];
BOOL ok;
NSError *setCategoryError = nil;
ok = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];

volume = audioSession.outputVolume;
volumeControl.value = volume;
NSTimer *volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateSound) userInfo:nil repeats:YES];

and the updateSound:

- (void)updateSound{
    volume = audioSession.outputVolume;
    volumeControl.value = volume;
}

How can i change the system volume when I move the slider? I tried to give a value to audioSession.outputVolume but of course, it doesn't work that way.

Deea B
  • 301
  • 6
  • 15
  • So the code @Merlevede suggested isn't working at all..does nothing. Is there any way I can influence the system volume using my slider? I am all out of ideas.. – Deea B Feb 24 '14 at 19:26

2 Answers2

3

You can listen to volume change notificaions

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(volumeChanged:)
 name:@"AVSystemController_SystemVolumeDidChangeNotification"
 object:nil];

And your selector would look like this

- (void)volumeChanged:(NSNotification *)notification
{
    float volume =
    [[[notification userInfo]
      objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
     floatValue];

    // Do stuff with volume
}
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • I am a beginner, so sorry if this seems like a stupid question.. I tried to add the first bit of code to viewDidLoad after I started the audio session, but I get nothing when pushing the volume buttons. I NSLog the volume float after the comment in the method volumeChanged. It should log something...right? Am I doing something wrong? – Deea B Feb 24 '14 at 17:42
  • According to [this link](http://stackoverflow.com/questions/7749030/objective-c-volumechanged-wasnt-called-even-add-a-observer-for-avsystemcontrol) and [this link](http://stackoverflow.com/questions/7528443/iphone-detect-volume-keys-press) you need to initialize an Audio Session for this call to work. – Merlevede Feb 24 '14 at 17:52
  • Yes..I tried to initialize audio session like this `AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionSetActive(true); ` but still nothing. Also I get notifications for both rows: deprecated in iOS7 – Deea B Feb 24 '14 at 18:02
0

Finally found the solution to what I was trying to do here

I removed all above code and included MediaPlayer framework in my .h file and this code in my .m file:

   UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(30, 350, 260, 20)];
    wrapperView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:wrapperView];

    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: wrapperView.bounds];
    [wrapperView addSubview:volumeView];

Hope this helps someone else as well.

Deea B
  • 301
  • 6
  • 15