简体   繁体   English

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

[英]Play Audio when device in silent mode - ios swift

I am creating an application using xcode 7.1, swift.我正在使用 xcode 7.1,swift 创建一个应用程序。 I want to play an audio.我想播放音频。 Everything is fine.一切安好。 Now my problem I want to hear sound when the device in silent mode or muted.现在我的问题是我想在设备处于静音模式或静音时听到声音。 How can I do it?我该怎么做?

I am using the following code to play audio我正在使用以下代码播放音频

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

            currentAudio!.currentTime = 0

            currentAudio!.play();

Put this line before calling play() method of AVPlayer.将此行放在调用 AVPlayer 的play()方法之前。

In Objective C在目标 C

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

In Swift在斯威夫特

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

Swift 5斯威夫特 5

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

You can use AppDelegate class.您可以使用AppDelegate类。

For enable sound (for audio or video) when device is in silent mode use AVAudioSessionCategoryPlayback :要在设备处于静音模式时启用声音(音频或视频),请使用AVAudioSessionCategoryPlayback

func applicationDidBecomeActive(_ application: UIApplication) {

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch {
        print("AVAudioSessionCategoryPlayback not work")
    }
}

For disable sound when device is in silent mode (for example when we answer the phone call) use AVAudioSessionCategorySoloAmbient :要在设备处于静音模式时禁用声音(例如当我们接听电话时),请使用AVAudioSessionCategorySoloAmbient

func applicationWillResignActive(_ application: UIApplication) {

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
    } catch {
        print("AVAudioSessionCategorySoloAmbient not work")
    }
}

Swift 3.0 and Xcode > 8 Swift 3.0 和 Xcode > 8

Play sound in Video when device is on RINGER mode and SLIENT mode当设备处于 RINGER 模式和 SLIENT 模式时在视频中播放声音

 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斯威夫特 4

use this line before play video在播放视频之前使用此行

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

Swift 4.2斯威夫特 4.2

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

斯威夫特 5.0.1

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

And add this to your AppDelegate's applicationDidBecomeActive section并将其添加到您的 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)
        }
    }

You can go through this, it will help you out你可以通过这个,它会帮助你

When you use following audio session categories, sounds will not be muted on iOS: AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord当您使用以下音频会话类别时,iOS 上的声音不会被静音: AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord

Example例子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM