【问题标题】:Play Audio when device in silent mode - ios swift当设备处于静音模式时播放音频 - ios swift
【发布时间】:2016-05-19 07:29:50
【问题描述】:

我正在使用 xcode 7.1 快速创建一个应用程序。我想播放音频。一切顺利。现在我的问题是我想在设备处于静音模式或静音时听到声音。我该怎么做?

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

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

            currentAudio!.currentTime = 0

            currentAudio!.play();

【问题讨论】:

    标签: ios swift audio


    【解决方案1】:

    你可以使用AppDelegate类。

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

    func applicationDidBecomeActive(_ application: UIApplication) {
    
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch {
            print("AVAudioSessionCategoryPlayback not work")
        }
    }
    

    要在设备处于静音模式时(例如接听电话时)禁用声音,请使用AVAudioSessionCategorySoloAmbient

    func applicationWillResignActive(_ application: UIApplication) {
    
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
        } catch {
            print("AVAudioSessionCategorySoloAmbient not work")
        }
    }
    

    【讨论】:

    • 启用“AVAudioSessionCategorySoloAmbient”,播放器控件中的画中画消失了!
    【解决方案2】:

    在调用 AVPlayer 的 play() 方法之前放置这一行。

    在目标 C 中

    [[AVAudioSession sharedInstance]
                setCategory: AVAudioSessionCategoryPlayback
                      error: nil];
    

    在 Swift 中

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

    斯威夫特 5

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

    【讨论】:

    • 它工作了吗?我需要初始化所有屏幕还是使用第一个视图控制器就足够了?
    • 真棒拯救了我的一天
    • 完美运行,谢谢!
    • 导入为:#import <AVFoundation/AVFoundation.h>
    • Small nitpick:对于“catch”,您无需指定(let error),因为 swift 会为您执行此操作。
    【解决方案3】:

    Swift 5.0.1

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

    【讨论】:

      【解决方案4】:
      import AVKit
      

      并将其添加到您的 AppDelegate 的 applicationDidBecomeActive 部分

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

      【讨论】:

        【解决方案5】:

        斯威夫特 4.2

           do {
                try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
            } catch let error {
                print("Error in AVAudio Session\(error.localizedDescription)")
            }
        

        【讨论】:

          【解决方案6】:

          斯威夫特 4

          在播放视频前使用此行

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

          【讨论】:

          • 仅适用于iOS10+
          【解决方案7】:

          Swift 3.0 和 Xcode > 8

          当设备处于 RINGER 模式和 SLIENT 模式时在 Video 中播放声音

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

          【讨论】:

          • 你好。这是错误的:catch _ as NSError。在这种情况下只需使用catch。不要向下转换为丢弃的值,这没有意义。
          【解决方案8】:

          你可以通过这个,它会帮助你的

          当您使用以下音频会话类别时,iOS 上不会静音:AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord

          示例

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

          【讨论】:

          • 你能分享一些例子吗
          • OP 正在使用 Swift 2(他们在问题中提到了 Xcode 7.1)。您的示例适用于 Swift 1。
          猜你喜欢
          • 2015-04-14
          • 1970-01-01
          • 1970-01-01
          • 2016-02-17
          • 2012-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多