Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

iphone - How can I detect whether an iOS device is in silent mode or not?

I've been looking for hours and I can't seem to find any API that explains how to detect whether an iPhone ringer is in silent mode or not. This is what I'm talking about:

iPhone Ringer Switch

Is there anyway I can find out whether an iOS device's silent mode is on or off using Swift code? I'm specifically looking for iOS8 and up since the iOS5 ones have been deprecated. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think there is and you probably don't need to directly call an API to detect if the device is muted or not. What you need to know is this:

When playing a sound, you will do something like:

        try AVAudioSession.sharedInstance().setCategory({AVAudioSessionCategory})
        try AVAudioSession.sharedInstance().setActive(true)
        audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()

In the {AVAudioSessionCategory}, you can specify:

AVAudioSessionCategorySoloAmbient: Your audio is silenced by screen locking and by the Silent switch

AVAudioSessionCategoryPlayBack: Your Audio continues with the Silent switch set to silent or when the screen locks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...