Callback when window has been closed by user

I have an application running that plays an audio file. I am using net.beadsproject.beads to get an audio-environment. If I close the window, the audio-playback is still running, since it runs in a different thread. I have to kill the processes in Intellij. Is there a callback I can use, when the window has been closed to manually stop the audio-thread?

Hi! Welcome to the forum :slight_smile:

I see Program.kt has these window events:

enum class WindowEventType {
    MOVED,
    RESIZED,
    FOCUSED,
    UNFOCUSED,
    MINIMIZED,
    RESTORED,
}

Could a CLOSE event be added? @edwin

It sounds useful to be able to clean things up…

Ah yeah. That’s a good one. Until that’s there you can use JVM shutdown hooks. For example: https://github.com/openrndr/openrndr/blob/master/openrndr-openal/src/main/kotlin/AudioPlayer.kt#L23

    init {
    samplePlayer = SamplePlayer(context, SampleManager.sample(samplePath))
    context.out.addInput(samplePlayer)

    Runtime.getRuntime().addShutdownHook(object : Thread() {
        override fun run() {
            println("shutdown")
            samplePlayer.kill()
            context.stop()
        }
    })
}

If I close the window (CMD+Q Mac), the callback will not be fired. Only if I quit the process in Intellij.

Strange, can you double check that? JVM shutdown hooks work with all modes of termination on all platforms for me.

Confirmed. I am constantly working on the project and I always have to stop the process in Intellij. The code snippets is still in there, but the console only logs “shutdown” when stopping the process manually.

That sounds like there may be a non-daemon thread running that prevents the JVM from shutdown.

Maybe it is connected to the Driver I am using.

private val context: AudioContext = AudioContext(
    JavaSoundAudioIO(512),
    512,
    IOAudioFormat(44100.0f, 16, 0, 2)
)

I am happy to try any other option to play wav-files in openrndr.

The shutdown hook never runs, because it’s waiting for the sample player thread to “finish”. I don’t think there’s a way around it till there’s a window closed life-cycle event.

I made Beads play sound using OPENRNDR’s OpenAL sound interface a good while ago. Sort of an abandoned codebase: https://github.com/edwinRNDR/pellet/blob/master/src/main/kotlin/org/openrndr/pellet/Pellet.kt

I just submitted code that addresses https://github.com/openrndr/openrndr/issues/126

1 Like