Difference between revisions of "Sound"
From GiderosMobile
m |
|||
Line 10: | Line 10: | ||
Control of the playing sound is performed through the [[SoundChannel]] object. | Control of the playing sound is performed through the [[SoundChannel]] object. | ||
− | === | + | === Example === |
+ | '''Play a background music in loop''' | ||
<source lang="lua"> | <source lang="lua"> | ||
− | local sound = Sound.new(" | + | --local sound = Sound.new("bgmusicloop.wav") |
− | local | + | local sound = Sound.new("bgmusicloop.mp3") |
− | -- | + | print(sound:getLength()) |
− | channel: | + | |
+ | local channel = sound:play(26000, true, false) | ||
+ | |||
+ | print(channel:getPosition()) | ||
+ | channel:setPosition(20000) | ||
+ | print(channel:getPosition()) | ||
+ | channel:setPaused(false) | ||
+ | |||
+ | channel:addEventListener(Event.COMPLETE, function() print("complete") end) | ||
+ | |||
+ | local function onTimer() | ||
+ | -- print(channel:getPosition(), sound:getLength()) | ||
+ | collectgarbage() | ||
+ | end | ||
+ | |||
+ | local t = Timer.new(100) | ||
+ | t:addEventListener(Event.TIMER, onTimer) | ||
+ | t:start() | ||
+ | |||
+ | stage:addEventListener(Event.MOUSE_DOWN, function() | ||
+ | channel:setPaused(not channel:isPaused()) | ||
+ | channel:setPosition(10000) | ||
+ | -- channel:setPitch(1.5) | ||
+ | end) | ||
</source> | </source> | ||
Revision as of 22:36, 19 March 2023
Supported platforms:
Available since: Gideros 2011.6
Inherits from: Object
Description
The Sound class lets you load and play WAV, MP3, MOD, XM, S3M and IT sound files.
Control of the playing sound is performed through the SoundChannel object.
Example
Play a background music in loop
--local sound = Sound.new("bgmusicloop.wav")
local sound = Sound.new("bgmusicloop.mp3")
print(sound:getLength())
local channel = sound:play(26000, true, false)
print(channel:getPosition())
channel:setPosition(20000)
print(channel:getPosition())
channel:setPaused(false)
channel:addEventListener(Event.COMPLETE, function() print("complete") end)
local function onTimer()
-- print(channel:getPosition(), sound:getLength())
collectgarbage()
end
local t = Timer.new(100)
t:addEventListener(Event.TIMER, onTimer)
t:start()
stage:addEventListener(Event.MOUSE_DOWN, function()
channel:setPaused(not channel:isPaused())
channel:setPosition(10000)
-- channel:setPitch(1.5)
end)
See also
MethodsSound.new creates a new Sound object |
EventsConstants |