Home > database >  Possibly a error within ruby2d's library method regarding Audio input?
Possibly a error within ruby2d's library method regarding Audio input?

Time:12-04

 undefined method `pause' for #<Ruby2D::Sound:0x000002728db4b250 @path="musics/machinegun.mp3", @data=#<Object:0x000002728db4b1d8>> (NoMethodError)

pause or stop etc is a built-in method in ruby2d but I can not seem to make this work. I think that a specific part of ruby2d method might ran into an error because the play method, which is in the same category, still works fine but not the other aforementioned functions.

  assault_rifle = Sound.new('musics/machinegun.mp3')

    on :key_held do |event|
        if event.key == 'k'
          mainscreen.player_fire_bullet
          assault_rifle.play # this works!
        end

        if event.key == 'l'
            assault_rifle.pause # this is just to test my theory that the method is broken
        end
    end

    on :key_up do |event|
        if event.key == 'k'   
            assault_rifle.stop #method error here?
        end
    end

For more information regarding ruby2d 's audio, you can read it here: https://www.ruby2d.com/learn/audio/ I hope some one could look into this

CodePudding user response:

It's not a bug.

The Ruby2D::Sound class doesn't have a #pause method.

# frozen_string_literal: true

# Ruby2D::Sound

module Ruby2D
  # Sounds are intended to be short samples, played without interruption, like an effect.
  class Sound
    attr_reader :path
    attr_accessor :loop, :data

    #
    # Load a sound from a file
    # @param [String] path File to load the sound from
    # @param [true, false] loop If  true  playback will loop automatically, default is  false 
    # @raise [Error] if file cannot be found or music could not be successfully loaded.
    def initialize(path, loop: false)
      raise Error, "Cannot find audio file `#{path}`" unless File.exist? path

      @path = path
      @loop = loop
      raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
    end

    # Play the sound
    def play
      ext_play
    end

    # Stop the sound
    def stop
      ext_stop
    end

    # Returns the length in seconds
    def length
      ext_length
    end

    # Get the volume of the sound
    def volume
      ext_get_volume
    end

    # Set the volume of the sound
    def volume=(volume)
      # Clamp value to between 0-100
      ext_set_volume(volume.clamp(0, 100))
    end

    # Get the volume of the sound mixer
    def self.mix_volume
      ext_get_mix_volume
    end

    # Set the volume of the sound mixer
    def self.mix_volume=(volume)
      # Clamp value to between 0-100
      ext_set_mix_volume(volume.clamp(0, 100))
    end
  end
end

Note:

Sounds are intended to be short samples, played without interruption, like an effect.

If you look at the linked example they are using the Music class which does.

song = Music.new('song.mp3')

# Play the music
song.play

# Pause the music
song.pause

The top level doc also states:

Music is for longer pieces which can be played, paused, stopped, resumed, and faded out, like a background soundtrack.

  • Related