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.
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
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.