AudioKit / AudioKit

Audio synthesis, processing, & analysis platform for iOS, macOS and tvOS
http://audiokit.io
MIT License
10.7k stars 1.56k forks source link

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

Closed vycoder closed 6 months ago

vycoder commented 6 months ago

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

NickCulbertson commented 6 months ago

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
    }
vycoder commented 6 months ago

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?

NickCulbertson commented 6 months ago

The Cookbook showcases most of the various pieces of AudioKit and how to implement them. I started learning AudioKit with the Cookbook. You can download the project from GitHub and run it like any other Xcode project (unless you’re getting some weird compiler errors). You can also find some tutorials on YouTube that should help in going up the AudioKit learning curve.On May 4, 2024, at 11:11 PM, yev @.***> wrote: 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?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>