iOS 13 把声音控件改为了顶部的长条。
MPVolumeView
下的MPVolumeSlider
控制声音显示无效。
如图1,iOS 13 下,MPVolumeSlider
的MPVolumControllerSystemDataSource
是not available
的。
图1.png
注意:volume
在 iOS 7 之后禁止使用,是一个过期API,但还是可以使用的。
@property (nonatomic) float volume MP_DEPRECATED("Use MPVolumeView for volume control.", ios(3.0, 7.0));
图2 是正常情况下的。
图2.png
解决方案 :使用MPMusicPlayerController
来控制声音。
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
if (([musicPlayer respondsToSelector:@selector(setVolume:)]) && [[[UIDevice currentDevice] systemVersion] floatValue] >= 13.0) {
//消除警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[musicPlayer setVolume:cVolume];
#pragma clang diagnostic pop
}
iOS13之前的还是可以使用MPVolumeView控件来控制声音大小。
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
[volumeViewSlider setValue:cVolume animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
总结:
- iOS 13 使用
MPMusicPlayerController
的过期APIvolume
来修改声音的确不是上上策。有其他方案请留下评论吧! - MPVolumeView在修改声音的时候会打印如下日志,大家有办法给消除吗?
[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl outputDeviceDiscoverySessionDidChangeDiscoveryMode:]: Setting device discovery
mode to DiscoveryMode_Presence (client: MyAppName)