Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Playing a song with Gosu
- - By Andrek Date 2019-01-14 12:32
Is there a way to play a song with Gosu without start the main loop

Something like this: do
  song ="file.mp3")
  loop do
    break unless song.playing?

And why i get this error that works with Gosu::Song:

song ="file.mp3") # => RuntimeError: File contains data in an unknown format.

Thanks a lot! :)
Attachment: alert2.mp3 (33k)
Parent - - By jlnr (dev) Date 2019-01-14 15:35
I am not sure if you can use Gosu::Song on a background thread; Gosu is designed to be used on the main thread (for now). However, there is a Gosu::Song.update method that should be pretty much what you are looking for, if you have a way of regularly calling it from the main thread.

The MP3 loading bug should have been fixed in Gosu 0.14.3. If you are still experiencing this issue, can you please attach the MP3 file to your post?
Parent - By jlnr (dev) Date 2019-01-14 15:41
There is definitely still a difference in the file type detection between Song and Sample. I have just tried to reproduce this with an MP3 file on my desktop, and it only worked in Sample(!). Turns out this file is actually an M4A file with the wrong extension... Not sure why Song can't stream the file. It might be a macOS-specific error too, because I use Apple's AudioToolbox for file loading on macOS and iOS.

I have opened an issue here:
Parent - - By Andrek Date 2019-01-16 18:53 Edited 2019-01-19 23:34
i upload the mp3 file and update my gosu gem and now i can't play any sound, this does not works:

class MW < Gosu::Window
  def initialize
    super 640, 480
    @song ="alert2.mp3")

window =

Well, in my project i'm using gosu to load image, no drawing, this is just an IA comparing images and i wanted use Gosu to play a sond when my IA found something, i'm using win32-sound gem(converting my mp3 to wav) now. I used a Thread just like a example because i'm using gosu in the main thread without calling a Gosu::Window of course:

if compare_img(img) == :face"alert2.mp3").play


Edit: adding, sorry i forgot it.
Parent - - By cyberarm Date 2019-01-18 18:56

require "gosu"

tone ="alert2.mp3").play

# Prevent program from exiting instantly
  sleep 0.1

This will play your tone every 2.5 seconds without needing a window. (Note the replacement of Song with Sample, Song would cut out after a few hundred milliseconds and not reliably play again.)
A playing Song/Sample does not appear to block, resulting in the program exiting before it even starts playing, hence the while loop here.
Parent - - By Andrek Date 2019-01-19 23:43
Thanks! your code works for me and i happy now, when I update gosu, stopped playing sounds, the error that I mentioned before but after I restart everything is fixed.
Parent - - By jlnr (dev) Date 2019-01-20 11:37
Great to hear. So alert2.mp3 works both as a Sample and Song now?
Parent - By Andrek Date 2019-01-22 23:10
Right! 100% working :)
Up Topic Gosu / Gosu Exchange / Playing a song with Gosu

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill