SDLmixer: provide Mix_ChannelFinished working example

Issue #86 new
JP LeBreton created an issue

I'd like to create a callback when a sound finishes playing, and it doesn't appear to work like anything else I've encountered using pysdl2 so far.

http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC37

When I call Mix_ChannelFinished(my_func) where my_func is a standard Python function, I get this error:

ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected CFunctionType instance instead of method

My attempt to give it a CFunctionType instance looks like this:

MYFUNCTYPE = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.POINTER(ctypes.c_int))
my_c_func = MYFUNCTYPE(my_python_func)
sdlmixer.Mix_ChannelFinished(my_c_func)

but this gives the error:

ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected CFunctionType instance instead of CFunctionType

I'm sure I'm doing something very simple wrong here, but it's non-obvious enough that it would be nice to have an example of how to do this properly in pysdl2's excellent docs!

Comments (3)

  1. JP LeBreton reporter

    Attached: script creating a Mix_HookMusicFinished callback for music, and a Mix_ChannelFinished for a sound effect (sample/chunk). The music callback works as expected, but the sound callback segfaults running Mix_ChannelFinished - see the comment.

  2. Marcus von Appen repo owner

    Your Mix_ChannelFinished signature is wrong. It does not return a value and requires a c_int as argument

    MYFUNCTYPE = ctypes.CFUNCTYPE(None, ctypes.c_int)
    my_c_func = MYFUNCTYPE(my_python_func)
    sdlmixer.Mix_ChannelFinished(my_c_func)
    

    should do the trick.

  3. JP LeBreton reporter

    Excellent, thank you! Would you like me to clean up my above code and submit it as an example?

  4. Log in to comment