You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift Xcode中SwiftySound音频叠加播放问题求助

Fixing Audio Overlap with SwiftySound in Swift/Xcode

Hey there! I’ve run into this exact annoying overlap issue with SwiftySound before—nothing ruins an app’s vibe like layered background tracks when navigating screens. Let’s walk through a few solid solutions to get this sorted:

The cleanest way to avoid duplicate playback is to centralize your audio logic into a singleton. This ensures only one instance of your background music ever plays, no matter how many times you trigger the play command across different screens.

Here’s how to set it up:

import SwiftySound

class AudioManager {
    // Singleton instance—only one exists across the app
    static let shared = AudioManager()
    private var backgroundTrack: Sound?
    
    // Prevent external code from creating new instances
    private init() {}
    
    func startBackgroundMusic() {
        // Only play if the track isn’t already running
        guard backgroundTrack?.isPlaying != true else { return }
        
        backgroundTrack = Sound.play(
            file: "bensound-summer",
            fileExtension: "mp3",
            numberOfLoops: -1
        )
    }
    
    func stopBackgroundMusic() {
        backgroundTrack?.stop()
        backgroundTrack = nil
    }
}

Then update your viewDidLoad code to use the manager:

override func viewDidLoad() {
    super.viewDidLoad()
    AudioManager.shared.startBackgroundMusic()
}

Now, no matter which screen calls startBackgroundMusic(), it’ll only kick off the track once.

2. Check Playback State Before Playing

If you don’t want to set up a singleton, you can directly check if SwiftySound has active playback before triggering a new play command:

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Only start the track if no audio is currently playing
    if !Sound.isPlaying() {
        Sound.play(
            file: "bensound-summer",
            fileExtension: "mp3",
            numberOfLoops: -1
        )
    }
}

Note: This checks all active SwiftySound instances, so use this only if you’re only playing one audio track at a time (no sound effects overlapping with background music).

3. Stop Audio When Leaving the Screen (If Needed)

If you want the music to stop when navigating away (and restart when returning), add this to your view controller:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    Sound.stopAll()
}

This works great if each screen has its own unique background track, but isn’t ideal if you want continuous music across screens.

Bonus: Initialize Music in App/Scene Delegate

For truly persistent background music that runs across your entire app lifecycle, initialize it in your SceneDelegate (or AppDelegate for older projects) instead of individual view controllers:

func sceneDidBecomeActive(_ scene: UIScene) {
    AudioManager.shared.startBackgroundMusic()
}

This way, the music starts when the app becomes active and won’t restart every time you push a new view controller.

内容的提问来源于stack exchange,提问作者MasonPK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:46:08