     // Listen to changes in audio configuration
     // and restart the audio engine if it stops and should be playing
     @objc fileprivate static func audioEngineConfigurationChange(_ notification: Notification) {
-
-        if shouldBeRunning && !engine.isRunning {
-            do {
-                try engine.start()
-            } catch {
-                AKLog("couldn't start engine after configuration change \(error)")
-            }
-        }
-
+        DispatchQueue.main.async {
+            if shouldBeRunning && !engine.isRunning {
+                do {
+                    try engine.start()
+                } catch {
+                    AKLog("couldn't start engine after configuration change \(error)")
+                }
+            }
+        }
     }
 
     // Restarts the engine after audio output has been changed, like headphones plugged in.
     @objc fileprivate static func restartEngineAfterRouteChange(_ notification: Notification) {
-        if shouldBeRunning {
-            do {
-                try self.engine.start()
-                // Sends notification after restarting the engine, so it is safe to resume AudioKit functions.
-                if AKSettings.notificationsEnabled {
-                    NotificationCenter.default.post(
-                        name: Notification.Name(rawValue: AKNotifications.engineRestartedAfterRouteChange),
-                        object: nil,
-                        userInfo: (notification as NSNotification).userInfo)
-
-                }
-            } catch {
-                AKLog("error restarting engine after route change")
+        DispatchQueue.main.async {
+            if shouldBeRunning {
+                do {
+                    try self.engine.start()
+                    // Sends notification after restarting the engine, so it is safe to resume AudioKit functions.
+                    if AKSettings.notificationsEnabled {
+                        NotificationCenter.default.post(
+                            name: Notification.Name(rawValue: AKNotifications.engineRestartedAfterRouteChange),
+                            object: nil,
+                            userInfo: (notification as NSNotification).userInfo)
+
+                    }
+                } catch {
+                    AKLog("error restarting engine after route change")
+                }
             }
         }
     }

                        // and restart the engine if it is stopped.
                        NotificationCenter.default.addObserver(
                            self,
                            selector: #selector(audioEngineConfigurationChange),
                            name: NSNotification.Name.AVAudioEngineConfigurationChange,
                            object: engine)

                    } else if AKSettings.useBluetooth {

#if !os(tvOS)
import CoreAudioKit
#endif

import Dispatch

public typealias AKCallback = (Void) -> Void

