我正在开发一个 GUI 音乐播放器,我的程序需要按顺序播放所有曲目。当我按下右箭头键时,我还需要暂停曲目:
def button_down(id)
case id
when Gosu::MsLeft
@locs = [mouse_x, mouse_y]
if area_clicked(mouse_x, mouse_y)
@show_tracks = true
@track_index = 0
@track_location = @albums[3].tracks[@track_index].location.chomp
playTrack(@track_location, @track_index)
end
when Gosu::KbRight
@song.pause()
when Gosu::KbLeft
@song.play()
end
end
我碰壁了,因为我的更新方法这样做了:
def update
if (@song != nil)
count = @albums[3].tracks.length
track_index = @track_index + 1
if (@song.playing? == false && track_index < count)
track_location = @albums[3].tracks[track_index].location.chomp
playTrack(track_location, track_index)
end
end
end
它检查歌曲是否正在播放,如果它是假的,它会移动到下一首曲目。因此,我的暂停按钮本质上是一个跳过曲目按钮。if (@song.playing? == false)
当第一首曲目结束时,我需要播放第二首曲目。
这是 playTrack 方法:
def playTrack(track_location, track_index)
@song = Gosu::Song.new(track_location)
@track_name = @albums[3].tracks[track_index].name.to_s
@album_name = @albums[3].title.to_s
@song.play(false)
@track_index = track_index
end