如何在不跳过音乐曲目的情况下暂停它们



我正在开发一个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)在第一首曲目结束时播放第二首曲目。

这是播放跟踪方法:

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

如果歌曲暂停或停止,@song.playing?将被false,因此您无法以这种方式区分这些状态。

幸运的是,还有Song#paused?

如果此歌曲是当前歌曲并且播放已暂停,则返回 true。

在代码方面,你会写这样的东西:

if @song.paused?
@song.play
elsif !@song.playing? && track_index < count
# just like before
end

最新更新