Sound.new: Difference between revisions

From GiderosMobile
No edit summary
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
<languages />
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
'''Class:''' [[Sound]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/Sound|Sound]]<br/>
 
=== <translate>Description</translate> ===
=== Description ===
<translate><br />
Creates a new '''Sound''' object.
Creates a new [[Special:MyLanguage/Sound|Sound]] object.<br />
<syntaxhighlight lang="lua">
<br /></translate>
Sound.new(filename)
<source lang="lua">
Sound.new(data,rate,stereo)
Sound.new(filename)
</syntaxhighlight>
</source>
 
=== <translate>Parameters</translate> ===
=== Parameters ===
'''filename''': (string) <translate>The name of the sound file to be loaded.</translate> <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()