Not logged inGosu Forums
Forum back to libgosu.org 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 window.show

Something like this:

Thread.new do
  song = Gosu::Song.new("file.mp3")
  song.play
  loop do
    song.update
    break unless song.playing?
  end
end


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

song = Gosu::Simple.new("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: https://github.com/gosu/gosu/issues/484
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 = Gosu::Song.new("alert2.mp3")
    @song.play
  end
end

window = MW.new
window.show


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
  Gosu::Song.new("alert2.mp3").play
end


Thanks!

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

require "gosu"

tone = Gosu::Sample.new("alert2.mp3").play

# Prevent program from exiting instantly
while(tone.playing?)
  sleep 0.1
end


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