以下是为APP添加控制音量的步骤:
1、导入依赖库,如果不导入就会出现clang: error: linker command failed with exit code 1 (use -v to see invocation)这个错误,如下图所示:
2、导入系统框架#import <MediaPlayer/MediaPlayer.h>
3、实现代码
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
float systemVolume = volumeViewSlider.value;
[volumeViewSlider setValue:[[def objectForKey:@"systemVolume"] floatValue] animated:NO];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
if (volumeViewSlider.value == 1) {
volumeViewSlider.value = 0;
}else{
volumeViewSlider.value += 0.1f;
systemVolume += 0.1f;
NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
[de setFloat:systemVolume forKey:@"systemVolume"];
}
NSLog(@"%f",volumeViewSlider.value);
但是通过以上代码调节音量不能控制相机拍照。