Swift 3.0 AVAudioPlayer 通过音乐播放音频 [英] Swift 3.0 AVAudioPlayer Playing audio over music
本文介绍了Swift 3.0 AVAudioPlayer 通过音乐播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前这是我用来在按下按钮时播放声音的代码.
Currently this is the code I'm using to play my sound during the button press.
var player: AVAudioPlayer?
let url = Bundle.main.url(forResource: "Sound/yatch", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
我试图让这个音频在当前播放的内容上运行(例如 Spotify、pandora、音乐).我该怎么做?
Im trying to get this audio to run over whats currently playing (etc Spotify, pandora, Music). How would I go about doing this?
推荐答案
设置应用程序的音频会话以在当前播放的音频上播放:
Set the audio session of your application to play over the currently playing audio:
try? AVAudioSession.sharedInstance().setActive(true)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
根据 AVAudioSession
标头,AVAudioSessionCategoryAmbient
将播放音频和音乐等
According to the AVAudioSession
headers, AVAudioSessionCategoryAmbient
will play audio with music, etc.
/* Use this category for background sounds such as rain, car engine noise, etc.
Mixes with other music. */
public let AVAudioSessionCategoryAmbient: String
这篇关于Swift 3.0 AVAudioPlayer 通过音乐播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文