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)