ios app 开发 设置静音模式 的代码怎么写
时间: 2024-05-01 18:18:12 浏览: 14
以下是iOS App中设置静音模式的代码示例:
Objective-C版本:
```
#import <AVFoundation/AVFoundation.h>
// 静音模式开关控制器
- (IBAction)silentSwitchValueChanged:(id)sender {
UISwitch *silentSwitch = (UISwitch *)sender;
if (silentSwitch.isOn) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
}
}
```
Swift版本:
```
import AVFoundation
// 静音模式开关控制器
@IBAction func silentSwitchValueChanged(_ sender: Any) {
if (sender as! UISwitch).isOn {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try! AVAudioSession.sharedInstance().setActive(true)
} else {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
try! AVAudioSession.sharedInstance().setActive(false)
}
}
```
以上为示例代码,并非完整代码,具体实现需要根据具体需求而定。