Sound.new: Difference between revisions

From GiderosMobile
removed language stuff
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 5: Line 5:
=== Description ===
=== Description ===
Creates a new '''Sound''' object.
Creates a new '''Sound''' object.
<source lang="lua">
<syntaxhighlight lang="lua">
Sound.new(filename)
Sound.new(filename)
</source>
Sound.new(data,rate,stereo)
</syntaxhighlight>


=== Parameters ===
=== Parameters ===
'''filename''': (string) the path of the sound file to be loaded<br/>
'''filename''': (string) the path of the sound file to be loaded<br/>
'''data''': (table) an array of values between -1 and 1 describing the waveform of the sound<br/>
'''rate''': (number) the sampling rate of the waveform<br/>
'''stereo''': (boolean) set to true if the array contains two channels<br/>
=== Examples ===
<syntaxhighlight lang="lua">
local snd = Sound.new("audio/sfx01.wav")
local channel = snd:play()
if channel then channel:setVolume(0.5) end
</syntaxhighlight>
'''Playlist'''
<syntaxhighlight lang="lua">
-- 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()
</syntaxhighlight>


{{Sound}}
{{Sound}}

Latest revision as of 18:05, 29 September 2026

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()