Hi,

I'm finding difficult to run AudioKit with latest Swift 5.2 through XCode 11.4 (I understand the forum is about Swift only and so my question is just regarding the syntax I used). Is there a way to find a more useful error message? Or is the code syntax correct?

import SwiftUI
import AudioKit

struct ContentView: View {
    var oscillator = AKOscillator()
    var body: some View {
        VStack {
            Button("Play") {
                self.playSound()
            }
        }.onAppear {
            do {
                AudioKit.output = self.oscillator
                try AudioKit.start()
            } catch {
                print("audiokit init error")
            }
        }
    }
    
    private func playSound(){
        self.oscillator.amplitude = 1
        self.oscillator.frequency = 440
        self.oscillator.start()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

My apologies if this is XCode or AudioKit related but been trying to get some support without much success for the past weeks and before moving on to something else, I just want to make sure there's nothing I can do. I've also used GitHub and pulled some projects but the few I found don't compile or are missing libs, the goal was to use to study it.

Moved the Audiokit code to a separate struct, just to make sure:

// AudioPlayer.swift

import AudioKit

struct AudioPlayer {
    var oscillator = AKOscillator()
    init() {
        do {
            AudioKit.output = self.oscillator
            try AudioKit.start()
        } catch {
            print("audiokit init error")
        }
    }
    
    func playSound(){
        self.oscillator.amplitude = 1
        self.oscillator.frequency = 440
        self.oscillator.start()
    }
}

// ContentView.swift


import SwiftUI

struct ContentView: View {
    var audioPlayer: AudioPlayer = AudioPlayer()
    var body: some View {
        VStack {
            Button("Play") {
                self.audioPlayer.playSound()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thank you!

What’s the error?

The error I have is that the app crashes and I get:

| Error Domain=com.apple.dt.ultraviolet.service Code=12 "Rendering service was interrupted" UserInfo={NSLocalizedDescription=Rendering service was interrupted}

I have now tested as observable, just to make sure:

// ContentView.swift

import SwiftUI

struct ContentView: View {
    @ObservedObject var audioPlayer: AudioPlayer

    var body: some View {
        VStack {
            Button("Play") {
                self.audioPlayer.playSound()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(audioPlayer: AudioPlayer())
    }
}

// AudioPlayer.swift

import AudioKit
import Combine

class AudioPlayer: ObservableObject {
    let objectWillChange = PassthroughSubject<AudioPlayer, Never>()
    var isPlaying = false {
        didSet {
            objectWillChange.send(self)
        }
    }
    var oscillator = AKOscillator()
    
    init() {
        do {
            AudioKit.output = self.oscillator
            try AudioKit.start()
            isPlaying = true
        } catch {
            print("audiokit init error")
        }
    }
    
    func playSound(){
        self.oscillator.amplitude = 1
        self.oscillator.frequency = 440
        self.oscillator.start()
    }
}

// SceneDelegate.swift

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = ContentView(audioPlayer: AudioPlayer())
        
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}

Same error. Someone please reply

Does the crash happen when you run the app on a real device, simulator, playground or when you try to watch a preview in Xcode?