ios监听音量按键

来源:互联网 发布:golang beego 编辑:程序博客网 时间:2024/06/05 09:40

这个很简单,KVO监听AVAudioSession单例的outputVolume属性值就可以了

[[AVAudioSession sharedInstance] addObserver:selfforKeyPath:@"outputVolume"options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOldcontext:(void*)[AVAudioSession sharedInstance]];
 
 
 
 
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context{
    if(context == (__bridge void*)[AVAudioSession sharedInstance]){
        floatnewValue = [[change objectForKey:@"new"] floatValue];
        floatoldValue = [[change objectForKey:@"old"] floatValue];
        // TODO: 这里实现你的逻辑代码
    }
}


设置按键音效


- (IBAction)DO:(id)sender{

soundFile = [NSStringstringWithFormat:@"/001.mp3"];

[selfplaySound: soundFile];

}



-(void)playSound:(NSString*)soundKey{

NSString *path = [NSStringstringWithFormat:@"%@%@",[[NSBundlemainBundle] resourcePath],soundKey];

//NSLog(@"%@\n", path);

SystemSoundID soundID;

NSURL *filePath = [NSURLfileURLWithPath:path isDirectory:NO];

AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);

AudioServicesPlaySystemSound(soundID);

}



0 0
原创粉丝点击