美文网首页
【原】关于MPVolumeView获取系统音量

【原】关于MPVolumeView获取系统音量

作者: aggie1024 | 来源:发表于2019-03-15 16:50 被阅读0次

    项目中有个需求,需要用到slider控制系统音量,不是视频或者音频播放器,是蓝牙设备播报的音量控制,百度了下千篇一律全是一个方法,可是我试了一下并不好使,网上代码如下:
    //1.添加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;
    }
    }
    // change system volume, the value is between 0.0f and 1.0f
    [volumeViewSlider setValue:0.3f animated:NO];
    // send UI control event to make the change effect right now. 立即生效
    [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

    2.隐藏系统MPVolumeView

    volumeView.frame = CGRectMake(-1000, -100, 100, 100);
    volumeView.hidden = NO;

    3.根据硬件音量键修改音量-添加监听

    NSError *error;
    [[AVAudioSession sharedInstance] setActive:YES error:&error];
    // add event handler, for this example, it is volumeChange: method
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

    4.然后实现事件回调方法

    • (void)volumeChanged:(NSNotification *)notification
      {
      NSDictionary *userInfo = notification.userInfo;
      float value = [[userInfo objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
      NSLog(@"改变后的数据为:%f",value);
      }

    5.//最后记得在资源回收时取消掉事件监听

    • (void)dealloc
      {
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
      }

    以上的方法中,在硬件音量键修改的时候监听可以获取到,并能进行修改,可是在最初的时候获取不到系统音量。

    》》》》》我是转折符《《《《《《《《《《《
    看系统API有一个类MPVolumeView,直接两行代码就可以实现这些功能,只是UI展示是slider,也可以自定义设置slider样式,自定义代码如下:
    1.先引入框架

    import <MediaPlayer/MediaPlayer.h>

    2.viewDidLoad中添加方法

    • (void)viewDidLoad {
      [super viewDidLoad];
      self.title = @"扫码枪扫描中";
      [self addSoundSliderView];
      }

    3.实现方法
    //实现
    -(void)addSoundSliderView{

    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(RESIZE(80), RESIZE(70), SCREEN_WIDTH - RESIZE(160), RESIZE(30))];
    volumeView.backgroundColor = [UIColor redColor];
    
    //mini线条图片
    [volumeView setMinimumVolumeSliderImage:[UIImage imageNamed:@"pickupGoods_volumeMax"] forState:UIControlStateNormal];
    
    //max线条图片
    [volumeView setMaximumVolumeSliderImage:[UIImage imageNamed:@"pickupGoods_volumeMin"] forState:UIControlStateNormal];
    
    //中间圆标志
    [volumeView setVolumeThumbImage:[UIImage imageNamed:@"pickupGoods_volumeRound"] forState:UIControlStateNormal];
    [self.view addSubview:volumeView];
    
    //添加音量小喇叭提示图
    [self addSoundImageWithFrame:CGRectMake(RESIZE(50), RESIZE(65), RESIZE(17), RESIZE(27)) withImage:[UIImage imageNamed:@"pickupGoods_volumeDown"]];
    //添加音量大喇叭提示图
    [self addSoundImageWithFrame:CGRectMake(volumeView.wt_right + RESIZE(10), RESIZE(65), RESIZE(29), RESIZE(26)) withImage:[UIImage imageNamed:@"pickupGoods_volumeUp"]];
    

    }
    4.自定义添加喇叭图
    -(void)addSoundImageWithFrame:(CGRect )frame withImage:(UIImage *)image{

    UIImageView *soundImage = [[UIImageView alloc] init];
    soundImage.frame = frame;
    soundImage.image = image;
    [self.view addSubview:soundImage];
    

    }

    如上就可以实现了想要的效果,效果图如下:


    WechatIMG68.jpeg

    如有更好实现方式欢迎留言,转载请注明出处,谢谢!

    相关文章

      网友评论

          本文标题:【原】关于MPVolumeView获取系统音量

          本文链接:https://www.haomeiwen.com/subject/ayrrmqtx.html