Can't use volume button to mute on iOS7

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 13:49:48
user3907849

Same problem with my VoIP app...

My Solution: After everything is set up, set the category to Playback,

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Then set it back to PlayAndRecord,

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

This works for me, hope this helps someone who comes across the same problem.

msrdjan

A bit old question, but I also needed a solution for Gui13's question/comment:

We're not using any sort of control in our app, all volume interaction is based on volume buttons.. So I could maybe implement this but I'd need a way to tie it to the volume buttons. Is this possible?

Apple recommends using MPVolumeView, so I came up with this:

MPVolumeView *volumeView = [MPVolumeView new];
[self.view addSubview:volumeView];

and then:

__block UISlider *volumeSlider = nil;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[UISlider class]]) {
        volumeSlider = obj;
        *stop = YES;
    }
}];
[volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];

with:

- (void)handleVolumeChanged:(id)sender
{
    NSLog(@"%s - %f", __PRETTY_FUNCTION__, ((UISlider *)sender).value);
}

The [MPVolumeView new] code above inits MPVolumeView with no frame so, it's not visible in our self.view where it has been added as a subview, but it is important that it has to be added!

You can also init the MPVolumeView with the code:

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:self.view.frame];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];

which will also init the empty MPVolumeView (i.e. without RouteButton and VolumeSlider).

What's interesting is that UISlider subview (actually MPVolumeSlider, the subclass of UISlider) will still be found in the enumerateObjectsUsingBlock above.

This approach is also interesting because you can save the reference to volumeSlider and use it later to set volume from code, or your custom control:

Init and add to your view:

MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];

Find and save reference to UISlider:

__weak __typeof(self)weakSelf = self;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[UISlider class]]) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.volumeSlider = obj;
        *stop = YES;
    }
}];

Add target action for UIControlEventValueChanged:

[self.volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];

And then update your custom control when the volume has been changed (i.e. by the hardware volume controls):

- (void)handleVolumeChanged:(id)sender
{
    NSLog(@"%s - %f", __PRETTY_FUNCTION__, self.volumeSlider.value);
    self.myCustomVolumeSliderView.value = self.volumeSlider.value;
}

and also:

- (IBAction)myCustomVolumeSliderViewValueChanged:(id)sender {
    NSLog(@"set volume to: %f", self.myCustomVolumeSliderView.value);
    self.volumeSlider.value = self.myCustomVolumeSliderView.value;
}

Hope this helps someone (and that Apple doesn't remove MPVolumeSlider from MPVolumeView).

You have to use applicationMusicPlayer instead of iPodMusicPlayer to set the system volume:

#import <MediaPlayer/MediaPlayer.h>
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1; // max volume
musicPlayer.volume = 0; // min volume (mute)
musicPlayer.volume = 0.0625; // 1 bar on the overlay volume display

Apple is assuming that they know each and every way in which a volume will need to be administered.

There are, in fact, occasions when we want to change how the volume is controlled without forcing the user to have a volume slider on the screen, (alarm clocks and media players come to mind immediately). Yuou can take reference

https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPVolumeView_Class/Reference/Reference.html

http://ios-blog.co.uk/tutorials/controlling-system-output-volume-with-the-mpvolumeview-class-part-one/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!