Sound.new

From GiderosMobile
Revision as of 18:05, 29 September 2026 by MoKaLux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2011.6
Class: Sound

Description

Creates a new Sound object.

Sound.new(filename)
Sound.new(data,rate,stereo)

Parameters

filename: (string) the path of the sound file to be loaded
data: (table) an array of values between -1 and 1 describing the waveform of the sound
rate: (number) the sampling rate of the waveform
stereo: (boolean) set to true if the array contains two channels

Examples

local snd = Sound.new("audio/sfx01.wav")
local channel = snd:play()
if channel then channel:setVolume(0.5) end

Playlist

-- 1. Define your table of audio files
local playlist = {
    "audio/sound1.wav",
    "audio/sound2.wav",
    "audio/sound3.wav"
}

local currentIndex = 1
local currentChannel = nil

-- 2. Define the playback function
local function playNextTrack()
    -- Check if we reached the end of the playlist
    if currentIndex > #playlist then
        print("Playlist finished!")
        return
    end

    -- Load the current sound file
    local currentSound = Sound.new(playlist[currentIndex])
    
    -- Play the sound and catch its SoundChannel
    currentChannel = currentSound:play()

    -- Listen for when this specific audio track finishes playing
    currentChannel:addEventListener(Event.COMPLETE, function()
        -- Advance to the next file index
        currentIndex += 1
        
        -- Recursively trigger the next file
        playNextTrack()
    end)
end

-- 3. Start the playlist
playNextTrack()