如何暂停音乐曲目而不跳过它们

How to pause music tracks without skipping them

我正在开发一个 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
如果歌曲暂停或停止,

@song.playing? 将是 false,因此您无法以这种方式区分这些状态。

幸好还有Song#paused?:

Returns true if this song is the current song and playback is paused.

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

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