当设备处于静音模式时播放音频 - ios swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35289918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play Audio when device in silent mode - ios swift
提问by Amsheer
I am creating an application using 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?
我正在使用 xcode 7.1,swift 创建一个应用程序。我想播放音频。一切安好。现在我的问题是我想在设备处于静音模式或静音时听到声音。我该怎么做?
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();
回答by rushisangani
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)
}
回答by Saumil Shah
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)
}
回答by maxwell
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")
}
}
回答by faraz khonsari
Swift 4
斯威夫特 4
use this line before play video
在播放视频之前使用此行
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
回答by Yogendra Singh
Swift 4.2
斯威夫特 4.2
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
} catch let error {
print("Error in AVAudio Session\(error.localizedDescription)")
}
回答by Asha Antony
import AVKit
And add this to your AppDelegate's applicationDidBecomeActive
section
并将其添加到您的 AppDelegateapplicationDidBecomeActive
部分
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)
}
}
回答by bytepunch
Swift 5.0.1
斯威夫特 5.0.1
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
回答by Moin Shirazi
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()
}