AudioKit / AudioKit

Audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

Home Page:http://audiokit.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Oscillator only plays for a short time then stops in a physical device

vycoder opened this issue · comments

commented

macOS Version(s) Used to Build

macOS 13 Ventura

Xcode Version(s)

Xcode 14

Description

I've tried writing a very simple Oscillator that just plays a certain frequency. It's working on the emulator but once I deployed it to a my iPhone (12 mini, iOS version 17.4.1) it just plays for a short while but then stops (about 1 second). I've tried checking whether the Oscillator really started using isStarted but it prints out true.

Is there any configuration settings that I need to setup beforehand? I'm fairly new to AudioKit (and swift in general), was hoping someone can point me in the right direction. Here's my code:

public class HertzPlugin:CAPPlugin {
    let engine = AudioEngine()
    let oscillator = Oscillator()

    @objc func play(_ call: CAPPluginCall) {
        AudioKit.Settings.enableLogging = true
        let frequency = call.getFloat("value", 0)
        oscillator.stop()
        
        oscillator.frequency = frequency
        oscillator.amplitude = 1.0
        
        engine.output = oscillator
        do {
            try engine.start()
            
        } catch let err {
            print("error")
            Log(err)
        }
        print("playing...")
        oscillator.start()
        print("played \(oscillator.isStarted)") // always returns true
        call.resolve(["value": frequency])
    }
    
    @objc func stop(_ call: CAPPluginCall) {
        if (oscillator.isStarted == true) {
            oscillator.stop()
            engine.stop()
        }
        call.resolve()
    }
}

I'm running this using an M2 Macbook Air, MacOs Sonoma 14.4.1

Crash Logs, Screenshots or Other Attachments (if applicable)

No response

Try out the Cookbook Oscillator example and see if it is giving you the same issue. https://github.com/AudioKit/Cookbook

My guess is that you need to set the AVAudioSession category in your main app file like this:

init() {
#if os(iOS)
        do {
            Settings.bufferLength = .medium
            try AVAudioSession.sharedInstance().setPreferredIOBufferDuration(Settings.bufferLength.duration)
            try AVAudioSession.sharedInstance().setCategory(.playback,
                                                            options: [.mixWithOthers, .allowBluetoothA2DP])
            try AVAudioSession.sharedInstance().setActive(true)
        } catch let err {
            print(err)
        }
#endif
    }
commented

That worked really well! Closing this issue. Thanks for the help! Is the Cookbook the official place where to find all the necessary information regarding AudioKit? I'm very new to all of this, and I'm having trouble navigating the documentation. I don't even know how to run the Cookbook, any suggestions on where absolute beginners like me should start?