ios - 应用程序在静音模式下播放声音-Xcode

标签 ios xcode audio wav avaudiosession

我的应用程序中有一个功能可以播放声音效果。我非常确定,过去我的手机处于静音模式时,声音效果不会播放。但是现在他们做到了。我不确定为什么,需要知道我的手机是否出现故障或代码有问题。

        func playSound (Sound: String, Type: String) {

        //Prepare the sound file name & extension
        var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(Sound, ofType: Type)!)

        //Preparation to play
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)

        //Play audio

        var error: NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        }

最佳答案

当您使用以下音频 session 类别时,iOS上的声音不会被静音:AVAudioSessionCategoryPlaybackAVAudioSessionCategoryRecordAVAudioSessionCategoryPlayAndRecord
如果您不想在静音模式下播放声音效果,请使用AVAudioSessionCategoryAmbientAVAudioSessionCategorySoloAmbient。或者,只是不要触摸音频 session (不要注册一个),默认情况下,您的声音将在静音模式下被静音。

关于ios - 应用程序在静音模式下播放声音-Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801353/

相关文章:

c - 使用 Xcode 在 Mac 上的 C 中 fputs 崩溃

ios - didSelectRowAtIndexPath 不起作用,Swift 3

c# - 在 C# 中播放声音

audio - Gstreamer:从视频(flv)中提取音频,重新采样率和管道到流接收器 - 无法重新采样到流

java - 如何将图片和音频设置为mp4 ffmpeg?

ios - 对属性(property)设置者的行为感到困惑

iphone - NSPersistentStoreCoordinator 出现 nil 模型错误?

ios - iTunes Connect应用程序提交过程查询中的不受限制的Web访问选项

ios - 无法连接到 Xcode 5 和 Objective-C 中警报和操作表的每个按钮

objective-c - Xcode 符号图标...最终列表?