![](/img/trans.png)
[英]iOS / Play audio when screen is locked but not when phone on silent mode
[英]Play Audio when device in silent mode - ios swift
我正在使用 xcode 7.1,swift 創建一個應用程序。 我想播放音頻。 一切安好。 現在我的問題是我想在設備處於靜音模式或靜音時聽到聲音。 我該怎么做?
我正在使用以下代碼播放音頻
currentAudio!.stop()
currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));
currentAudio!.currentTime = 0
currentAudio!.play();
將此行放在調用 AVPlayer 的play()方法之前。
在目標 C
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: nil];
在斯威夫特
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
// report for an error
}
斯威夫特 5
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
} catch(let error) {
print(error.localizedDescription)
}
您可以使用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")
}
}
Swift 3.0 和 Xcode > 8
當設備處於 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)
}
斯威夫特 4
在播放視頻之前使用此行
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
斯威夫特 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
並將其添加到您的 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)
}
}
你可以通過這個,它會幫助你
當您使用以下音頻會話類別時,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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.