py-sdl2
py-sdl2 copied to clipboard
SDLmixer: provide Mix_ChannelFinished working example
Originally reported by: JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton)
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:
#!python
ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected CFunctionType instance instead of method
My attempt to give it a CFunctionType instance looks like this:
#!python
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:
#!python
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!
- Bitbucket: https://bitbucket.org/marcusva/py-sdl2/issue/86
Original comment by JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton):
Excellent, thank you! Would you like me to clean up my above code and submit it as an example?
Original comment by Marcus von Appen (Bitbucket: marcusva, GitHub: marcusva):
Your Mix_ChannelFinished signature is wrong. It does not return a value and requires a c_int as argument
#!python
MYFUNCTYPE = ctypes.CFUNCTYPE(None, ctypes.c_int)
my_c_func = MYFUNCTYPE(my_python_func)
sdlmixer.Mix_ChannelFinished(my_c_func)
should do the trick.
Original comment by JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton):
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.