简体   繁体   English

如何在不停止播放音乐的情况下在 swift 中播放声音?

[英]How can I play a sound in swift without stopping the playing music?

I'm using AVAudio to play a sound, but when I do so, the music (In the music app) stops.我正在使用 AVAudio 播放声音,但是当我这样做时,音乐(在音乐应用程序中)会停止。

    var audioPlayer: AVAudioPlayer?
    func playSound(sound: String, type: String) {
        if let path = Bundle.main.path(forResource: sound, ofType: type) {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                audioPlayer?.play()
            } catch {
                print("ERROR")
            }
        }
    }
    // Some code here
    playSound(sound: "resume", type: "m4a")

I want to make the sound act like a notification soud and that the music keeps playing.我想让声音像通知声音一样,并且音乐继续播放。 Any way to do this?有什么办法可以做到这一点?

Set your AVAudioSession to .duckOthers or .mixWithOthers :将您的AVAudioSession设置为.duckOthers.mixWithOthers

(Before you play sounds): (在播放声音之前):

do {
    try AVAudioSession.sharedInstance()
        .setCategory(.playback, options: .duckOthers)
    try AVAudioSession.sharedInstance()
        .setActive(true)
} catch {
    print(error)
}

You will first have to define properties of an AVAudioSession .您首先必须定义AVAudioSession的属性。 This lets you chose how to play the sound with the help of setCategory(_:mode:options:) .这使您可以在setCategory(_:mode:options:)的帮助下选择如何播放声音。 What you need is this:你需要的是这样的:

var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
    if let path = Bundle.main.path(forResource: sound, ofType: type) {
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers])
            try AVAudioSession.sharedInstance().setActive(true)

            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
            audioPlayer?.play()
        } catch {
            print("ERROR")
        }
    }
}
// Some code here
playSound(sound: "resume", type: "m4a")

Feel free to experiment with the setCategory function by passing different configuration options.随意通过传递不同的配置选项来试验setCategory function。 You can also read more about mixWithOthers , but the key point is:您还可以阅读有关mixWithOthers的更多信息,但关键是:

An option that indicates whether audio from this session mixes with audio from active sessions in other audio apps.指示来自此 session 的音频是否与来自其他音频应用程序中活动会话的音频混合的选项。

暂无
暂无

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

相关问题 我想在不停止另一个应用程序后台播放的音乐的情况下制作音效 - I want to make sound effects without stopping the music playing in the background in another app 使用AVAudioPlayer在后台播放声音而不停止ipod音乐 - Play sound in background using AVAudioPlayer without stopping ipod music iOS,迅速:立即播放背景音乐和音效 - iOS, swift: play background music and sound effects without delay iOS如何在后台播放音乐声音并暂停当前正在播放的音乐应用 - IOS How to play music sound in background and pause current playing music app 是否可以在不停止iPod音乐的情况下播放声音? - Is it possible to play sounds without stopping iPod music? 如果系统声音已经在播放,我不想播放游戏音乐 - I don't want play game music if system sound is already playing 如何在不停止当前音乐的情况下更改当前系统音量 - How can i change current system volume without making the current music stopping 如果不播放,则播放音乐,但如果已经播放,则不播放音乐(Xcode,Swift4) - Play music if not playing, but don't if it is already playing (Xcode,Swift4) 在不停止设备声音/音乐的情况下录制视频 - Recording a Video without stopping sound/music from device 如何播放随机声音而不重复?播放完所有文件后又重新开始新的声音循环 - how to play random sound without repeat ?after playing all file then again start new cycle of sound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM