ios隐藏系统音量图标,并监听系统声音

    MPVolumeView *volumeView = [[MPVolumeViewalloc]initWithFrame:CGRectMake(-100, -100,100,100)];

     [self.viewaddSubview:volumeView];

第二行代码非常重要,之前弄了好久都没效果。



另外:

设置监听

-(void)viewDidAppear:(BOOL)animated{    

    NSError *error;

    [[AVAudioSessionsharedInstance]setActive:YESerror:&error];

    [[AVAudioSessionsharedInstance]addObserver:selfforKeyPath:@"outputVolume"options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:(void *)[AVAudioSessionsharedInstance]];

}


退出界面的时候关闭监听

-(void)viewDidDisappear:(BOOL)animated{

    [superviewDidDisappear:animated];

    NSError *error;

    [[AVAudioSessionsharedInstance]setActive:NOerror:&error];

    [[AVAudioSessionsharedInstance]removeObserver:selfforKeyPath:@"outputVolume"context:(__bridgevoid *)([AVAudioSessionsharedInstance])];

}


实现KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    

    if(context == (__bridgevoid *)[AVAudioSessionsharedInstance]){

////ToDo        

    }


}


本人自建的一个iOS交流群165332140,欢迎各位大神进群交流指导。




你可能感兴趣的:(ios笔记)