iOS开发笔记iOS面试专题

iOS 13 MPVolumeView控制声音无效

2019-10-11  本文已影响0人  iOS俱哥

iOS 13 把声音控件改为了顶部的长条。

MPVolumeView 下的MPVolumeSlider 控制声音显示无效。
如图1,iOS 13 下,MPVolumeSliderMPVolumControllerSystemDataSourcenot 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];

总结:

  1. iOS 13 使用MPMusicPlayerController 的过期APIvolume来修改声音的确不是上上策。有其他方案请留下评论吧!
  2. MPVolumeView在修改声音的时候会打印如下日志,大家有办法给消除吗?
[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl outputDeviceDiscoverySessionDidChangeDiscoveryMode:]: Setting device discovery 
mode to DiscoveryMode_Presence (client: MyAppName)
上一篇下一篇

猜你喜欢

热点阅读