Currently, I have this issue where I need to load the music files into ruby so that it can be played. While it works with absolute path, it doesn't work when I use variables to represent the file path so that i don't have to manually type in the file path.
The code snippet is as follows:
def playTrack(track_name, album)
track = album.tracks.find {|t|t.name == track_name}
track_path= track.location.to_s
music_path= File.expand_path(track_path, File.dirname(__FILE__))
puts music_path
if track
@song = Gosu::Song.new(music_path.to_s)
@song.play(false)
end
end
These are the variables that I used to present the file paths:
track_path = sounds/01-Cracklin-rose.mp3
File.dirname(__FILE__) = c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer
DIR.pwd = c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work
the music file's path: C:\Users\ultra\OneDrive\Documents\schoolstuff\uni\sem 1\programming stuff\programming work\portfolio\Distinction Tasks\9.1.3D\musicplayer\sounds\01-Cracklin-rose.mp3
I was expecting the song to play as normal, however, it returned the error:
C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/gosu-1.4.5/lib/gosu/compat.rb:116:in `initialize': Could not parse audio file c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/sounds/01-Cracklin-rose.mp3 (RuntimeError)
: Couldn't open c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction T
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/gosu-1.4.5/lib/gosu/compat.rb:116:in `initialize'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:164:in `new'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:164:in `playTrack'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:231:in `block in button_down'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:223:in `each'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:223:in `each_with_index'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:223:in `button_down'
from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/gosu-1.4.5/lib/gosu/patches.rb:64:in `tick'
from c:/Users/ultra/OneDrive/Documents/schoolstuff/uni/sem 1/programming stuff/programming work/portfolio/Distinction Tasks/9.1.3D/musicplayer/gui_music_player.rb:241:in `<main>'
I tried using another variable to represent File.dirname(FILE), used DIR.pwd, used File?exist to debug (returns false for whatever reason even though the file path can be located and opened in browser), file path seems to be correct, but nothing seemed to work.