music plays on top of eachother in swift xcode

Basically, im making this app in xcode using swift, latest versions, and the problem is here:

 override func viewDidLoad() {
        super.viewDidLoad() "bensound-summer", fileExtension: "mp3", numberOfLoops: -1)

im using a third party library with a pod, called "SwiftySound". Its used to play audio through an app.

my goal is to have background music in my app play automatically on a infinite loop (Which the above code DOES represent)

but the problem is , when i click my button to go into another view controller, and i go back, it replays again on top of the previous loop, so two audio files are playing at the same time. I want it to just stay consitant across all view controllers.

Heres everything:

import UIKit
import AVFoundation
import SpriteKit
import SwiftySound

var Clicks = Int()

class ViewController: UIViewController {

    @IBOutlet weak var LabelClicks: UILabel!

    @IBAction func Button(_ sender: Any) {
        LabelClicks.text="\(Clicks)" + " Clicks!"
        UserDefaults.standard.set(LabelClicks.text, forKey: "clicks")
        UserDefaults.standard.set(Clicks, forKey: "amount")

    override func viewDidAppear(_ animated: Bool) {
        if let x = UserDefaults.standard.object(forKey: "amount") as? Int {
            Clicks = x

        if let y = UserDefaults.standard.object(forKey: "clicks") as? String {
            self.LabelClicks.text = y
    @IBAction func UpgradeButton(_ sender: Any) {
        performSegue(withIdentifier: "segue", sender: self)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let UpgradesController = segue.destination as! UpgradesViewController
        UpgradesController.clickString = LabelClicks.text!
    override func viewDidLoad() {
        super.viewDidLoad() "bensound-summer", fileExtension: "mp3", numberOfLoops: -1)
        // Do any additional setup after loading the view, typically from a nib.

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.


1 answer

  • answered 2018-04-14 15:08 Ratul Sharker

    Play the sound in AppDelegate's didFinishedLaunchingWithOptions

    as follows :

    class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
   "bensound-summer", fileExtension: "mp3", numberOfLoops: -1)
            return true

    If you want to stop the sound when the app become background, and play again while in foreground, you can take following approach:

    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.
   "bensound-summer", fileExtension: "mp3", numberOfLoops: -1)
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.