当设备处于静默模式时播放音频-ios swift

当设备处于静默模式时播放音频-ios swift,ios,swift,audio,Ios,Swift,Audio,我正在使用xcode 7.1 swift创建一个应用程序。我想播放一段音频。一切都很好。现在我的问题是,我想在设备处于静音模式或静音时听到声音。我怎么做 我正在使用以下代码播放音频 currentAudio!.stop() currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio",

我正在使用xcode 7.1 swift创建一个应用程序。我想播放一段音频。一切都很好。现在我的问题是,我想在设备处于静音模式或静音时听到声音。我怎么做

我正在使用以下代码播放音频

currentAudio!.stop()
            currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));

            currentAudio!.currentTime = 0

            currentAudio!.play();

你可以经历这一切,它会帮你走出困境

当您使用以下音频会话类别时,在iOS上声音将不会静音:
AVAudioSessionCategoryPlayback、AVAudioSessionCategoryRecord、AvaudioSessionCategoryPlayRecord

示例

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()
        }

在调用AVPlayer的play()方法之前,请先写这行

在目标C中

[[AVAudioSession sharedInstance]
            setCategory: AVAudioSessionCategoryPlayback
                  error: nil];
在Swift中

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
 }
 catch {
    // report for an error
 }
Swift 5

do {
      try AVAudioSession.sharedInstance().setCategory(.playback)
   } catch(let error) {
       print(error.localizedDescription)
   }

Swift 3.0和Xcode>8

当设备处于振铃模式和静音模式时,在视频中播放声音

 do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        //print("AVAudioSession Category Playback OK")
        do {
            try AVAudioSession.sharedInstance().setActive(true)
            //print("AVAudioSession is Active")
        } catch _ as NSError {
            //print(error.localizedDescription)
        }
    } catch _ as NSError {
        //print(error.localizedDescription)
    }

Swift 4

播放视频前请使用此行

try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
Swift 4.2


您可以使用
AppDelegate
class

在设备处于静默模式时启用声音(音频或视频)请使用:

func applicationDidBecomeActive(_ application: UIApplication) {

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch {
        print("AVAudioSessionCategoryPlayback not work")
    }
}
当设备处于静音模式时(例如,当我们接听电话时)禁用声音,请使用:

func applicationWillResignActive(_ application: UIApplication) {

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
    } catch {
        print("AVAudioSessionCategorySoloAmbient not work")
    }
}
并将其添加到AppDelegate的
ApplicationIDBecomeActive
部分

func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
                print("AVAudioSession is Active")
            } catch {
                print(error.localizedDescription)
            }
        } catch {
            print(error.localizedDescription)
        }
    }

Swift 5.0.1

try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)

你能分享一些例子吗?Op正在使用Swift 2(他们在问题中提到了Xcode 7.1)。您的示例是Swift 1。它是否有效?我是否需要在所有屏幕中进行日化,或者使用first view controller就足够了?太棒了,完美地保存了我的日常工作,谢谢!导入是:
#导入
小细节:对于“捕获”,您不需要指定(让错误),因为swift将为您执行此操作。您好。这是错误的:
catch\u作为n错误
。在这种情况下,只需使用
catch
。不要向下转换为丢弃的值,这没有意义。它仅适用于iOS10+
func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
                print("AVAudioSession is Active")
            } catch {
                print(error.localizedDescription)
            }
        } catch {
            print(error.localizedDescription)
        }
    }
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)