Sound
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Sound è un oggetto che emette suono.Quando viene posizionato in un BasePart o in un Attachment , questo oggetto emetterà il suo suono da quella parte di BasePart.Position o dall'allegato di Attachment.WorldPosition .In questo posizionamento, un Sound esibisce l'effetto Doppler, cioè la sua frequenza e l'inclinazione varia con la relativa velocità di movimento di qualsiasi allegato o parte a cui è allegato.Inoltre, il suo volume sarà determinato dalla distanza tra il lettore sonoro del client (per impostazione predefinita la posizione Camera ) e la posizione del parentdel suono.Per ulteriori informazioni, vedi RollOffMode .
Un suono è considerato "global" se non è genitoriale a un BasePart o a un Attachment.In questo caso, il suono si riproducirà allo stesso volume in tutto il Posto.
Campioni di codice
The code in this sample demonstrates how a sound parented to a Part or Attachment will play locally and experience volume drop off the further the player's camera is away from the part.
A part is instanced, and a sound is instanced and parented to the part. A click detector is set up on the part that will check if the sound is playing, using Sound.IsPlaying and play or stop the sound depending.
local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 3, 0)
part.BrickColor = BrickColor.new("Bright red")
part.Name = "MusicBox"
part.Parent = workspace
-- create a sound
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- decrease the emitter size (for earlier volume drop off)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- toggle the sound playing / not playing
clickDetector.MouseClick:Connect(function()
if not sound.IsPlaying then
part.BrickColor = BrickColor.new("Bright green")
sound:Play()
else
part.BrickColor = BrickColor.new("Bright red")
sound:Stop()
end
end)
Sommario
Proprietà
Questa proprietà è true quando il Sound è stato caricato dai server Roblox e è pronto a Giocare.
Proprietà read-only che restituisce true quando il Sound non sta giocando.
Proprietà read-only che restituisce true quando il Sound è in riproduzione.
Un intervallo che denota l'inizio e la fine del ciclo desiderato all'interno del PlaybackRegion, in secondi.
Imposta se la Sound ripetizione si ripeta o meno una volta che ha finito di giocare.
Quando true , il Sound verrà riprodotto quando verrà rimosso dall'esperienza.
Un numero tra 0 e 1000 che indica quanto è forte l'attuale riproduzione IndietroSound.
Un intervallo che denota un tempo di avvio e di stop desiderato all'interno del TimeLength, in secondi.
Se true , questa proprietà dà al tuo Sound accesso alle proprietà PlaybackRegion e LoopRegion che possono controllare più accuratamente il suo riproduzione.
Determina la velocità a cui un Sound Giocare, con valori più alti che fanno sì che il suono si riproduca più velocemente e ad una maggiore altezza.
Indica se il Sound sta attualmente giocando.
La distanza massima, in studs, il lettore di un client può essere dall'origine del suono e ancora sentirlo.Si applica solo a Sounds genitori a un BasePart o Attachment .
La distanza minima, in studs, a cui un Sound che è parentedato a un BasePart o Attachment inizierà a attenuarsi (riduzione del volume).
Controlla come il volume di un Sound che è parentedato a un BasePart o Attachment si attenua (scompare) man mano che diminuisce la distanza tra l'ascoltatore e il padre.
Il SoundGroup che è collegato a questo Sound .
ID del contenuto del file audio da associare al Sound .
La lunghezza del Sound in secondi.
Progresso del Sound in secondi. Può essere cambiato per spostare la posizione di riproduzione del Sound sia prima che durante la riproduzione.
Il volume del Sound .
Metodi
Eventi
Fuochi ogni volta che i Sound loop.
Si accende quando il Sound ha completato il riproduzione e si è fermato.
Si accende quando il Sound è caricato.
Si attiva ogni volta che il Sound viene interrotto utilizzando Pause() .
Si attiva ogni volta che il Sound viene riprodotto utilizzando Play() .
Si accende quando il Sound viene ripreso utilizzando Resume() .
Si accende quando il Sound viene interrotto utilizzando Stop() .
Proprietà
ChannelCount
IsLoaded
Questa proprietà è true quando il Sound è stato caricato dai server Roblox e è pronto a Giocare.Puoi usare questa proprietà e l'evento Loaded per verificare che un suono sia stato caricato prima di riprodurlo.
Campioni di codice
This simple function will verify a Sound has loaded by checking the Sound.IsLoaded property. If Sound.IsLoaded is false it will wait for the Loaded event before resuming.
It is important to check Sound.IsLoaded before connecting to the Sound.Loaded event, as if the sound has already loaded the Sound.Loaded event will not fire and the function will yield indefinitely.
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")
IsPaused
Questa proprietà read-only restituisce true quando il Sound non sta giocando.Nota che può restituire true se un suono è stato interrotto utilizzando Pause() , se è stato interrotto utilizzando Stop() , o il suono non è mai stato riprodotto.
Poiché IsPaused è in lettura solo, non può essere utilizzato per interrompere il suono; Stop() o Pause() dovrebbe essere utilizzato invece.
Campioni di codice
This code sample contains demonstrates when the Sound.IsPlaying and Sound.IsPaused properties will be true or false.
A sound is instanced in the Workspace and the Sound.IsLoaded property is checked to ensure it has loaded, if it has not the Sound.Loaded event is used to yield the script until the sound has.
As the sound is played, paused and stopped the Sound.IsPlaying and Sound.IsPaused properties are printed to demonstrate how they respond to each of these functions. Note Sound.IsPaused will always be true if even if the sound has been stopped rather than paused.
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
if not sound.isLoaded then
sound.Loaded:Wait()
end
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- false, true
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true
IsPlaying
Questa proprietà read-only restituisce vero quando il Sound sta giocando.
Poiché IsPlaying è in lettura solo, non può essere utilizzato per riprodurre il suono; Play() dovrebbe essere utilizzato invece.
Campioni di codice
This code sample contains demonstrates when the Sound.IsPlaying and Sound.IsPaused properties will be true or false.
A sound is instanced in the Workspace and the Sound.IsLoaded property is checked to ensure it has loaded, if it has not the Sound.Loaded event is used to yield the script until the sound has.
As the sound is played, paused and stopped the Sound.IsPlaying and Sound.IsPaused properties are printed to demonstrate how they respond to each of these functions. Note Sound.IsPaused will always be true if even if the sound has been stopped rather than paused.
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
if not sound.isLoaded then
sound.Loaded:Wait()
end
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- false, true
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- true, false
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true
LoopRegion
Un intervallo che denota l'inizio e la fine del ciclo desiderato all'interno del PlaybackRegion, in secondi.
Se Class.Sound.LoopRegion|LoopRegion.Min``>``Class.Sound.PlaybackRegion|PlaybackRegion.Min , il ciclo inizia da LoopRegion.Min .
Se Class.Sound.LoopRegion|LoopRegion.Min``<``Class.Sound.PlaybackRegion|PlaybackRegion.Min , il ciclo inizia da PlaybackRegion.Min .
Se Class.Sound.LoopRegion|LoopRegion.Max``>``Class.Sound.PlaybackRegion|PlaybackRegion.Max , il ciclo inizia a PlaybackRegion.Max .
Se Class.Sound.LoopRegion|LoopRegion.Max``<``Class.Sound.PlaybackRegion|PlaybackRegion.Max , il ciclo inizia a esattamente quel momento.
Se Class.Sound.LoopRegion|LoopRegion.Min``==``Class.Sound.LoopRegion|LoopRegion.Max , il Sound utilizza la proprietà PlaybackRegion invece.
Looped
Questo imposta se la Sound ripetizione avvenga o meno una volta che ha finito di giocare.I suoni loop sono adatti per una gamma di applicazioni, tra cui musica e suoni ambientali di sottofondo.
L'evento DidLoop può essere utilizzato per tracciare il numero di volte in cui il suono si è ripetuto in loop.
Campioni di codice
This code sample includes a function that will play a sound and allow it to loop for a given number of times before stopping it.
local function loopNTimes(sound, numberOfLoops)
if not sound.IsPlaying then
sound.Looped = true
local connection = nil
connection = sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print(numOfTimesLooped)
if numOfTimesLooped >= numberOfLoops then
-- disconnect the connection
connection:Disconnect()
-- stop the sound
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)
PlayOnRemove
Quando true , il Sound si giocherà quando verrà rimosso dall'esperienza genitorializzando il Sound o uno se i suoi antenati a nil .Questo significa che tutto quanto segue causerà il suono di riproduzione quando PlayOnRemove è true :
- sound:Destroy()
- sound.Parent = nil
- sound.Parent.Parent = nil
PlaybackLoudness
Un numero tra 0 e 1000 che indica quanto è forte l'attuale riproduzione IndietroSound.Questa proprietà riflette l'ampiezza del riproduzione del suono nell'istanza del tempo in cui viene letto.
Campioni di codice
In this sample Sound.PlaybackLoudness is used to create an amplitude bar that shows the amplitude of a sound playing.
This code sample should be placed in StarterPlayerScripts.
A simple GUI is created, a frame holding that bar and a frame containing the bar. A Sound is then played and the size of the bar is set to reflect the Sound.PlaybackLoudness on a loop.
-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- create a holder for our bar
local frame = Instance.new("Frame")
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.Size = UDim2.new(0.3, 0, 0.05, 0)
frame.Parent = screenGui
-- create a bar
local bar = Instance.new("Frame")
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
bar.Parent = frame
-- create a sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- define a max loudness
local maxLoudness = 30
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
end
PlaybackRegion
Un intervallo che denota un tempo di avvio e di stop desiderato all'interno del TimeLength, in secondi.
Se PlaybackRegion.Min``>``0 , il suono inizia a suonare dall'ora PlaybackRegion.Min.
Se PlaybackRegion.Min``<``0 , il suono inizia a giocare da 0 .
Se PlaybackRegion.Max``>``Class.Sound.TimeLength , il suono si ferma a Sound.TimeLength .
Se PlaybackRegion.Max``<``Class.Sound.TimeLength , il suono si ferma a esattamente quel momento.
Se Class.Sound.PlaybackRegion|PlaybackRegion.Min``==``Class.Sound.PlaybackRegion|PlaybackRegion.Max , questa proprietà è Inattivo, Inattiva, Inattivi.
PlaybackRegionsEnabled
Se true , questa proprietà dà al tuo Sound accesso alle proprietà PlaybackRegion e LoopRegion che possono controllare più accuratamente il suo riproduzione.
PlaybackSpeed
Determina la velocità a cui un Sound Giocare, con valori più alti che fanno sì che il suono si riproduca più velocemente e ad una maggiore altezza.
Campioni di codice
In this example a Sound is played in the Workspace. The PlaybackSpeed property is increased and decreased at intervals to demonstrate its impact on the playback of the Sound.
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x faster
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x slower
task.wait(5)
sound.PlaybackSpeed = 1 -- default
Playing
Indica se il Sound è attualmente in riproduzione. Questa proprietà può essere attivata/disattivata e questa proprietà si replica sempre.
Nella finestra Proprietà di Studio, mentre in modalità Modifica , attivando Playing a true non inizia a riprodursi il suono, ma il suono inizierà a riprodursi durante l'Tempo esecuzione.
Questa proprietà non deve essere confusa con IsPlaying che è una Proprietàread-only.
Nota che quando Playing è impostato su false , la proprietà TimePosition del suono non verrà Ripristina, il che significa che quando Playing viene nuovamente impostato su true , l'audio continuerà dal punto in cui è stato interrotto quando è stato fermato.Tuttavia, se la funzione Play() viene utilizzata per riprendere il suono, la posizione temporale verrà ripristinata su 0 .
Campioni di codice
This sample demonstrates how the Sound.Playing property can be used to start and stop playback of a sound.
A sound is instanced in the Workspace and playback is started by setting Sound.Playing to true. After ten seconds the playback is stopped by setting Sound.Playing to false. When the playback is again resumed using Sound.Playing it resumes at the previous Sound.TimePosition it was stopped at. This is demonstrated by printing the TimePosition property immediately after resuming the sound.
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("playing sound")
sound.Playing = true
task.wait(10)
print("stopping sound")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("resumed at time position: " .. tostring(timePosition)) -- c. 10 seconds
RollOffMaxDistance
La distanza massima, in studs, il lettore di un client può essere dall'origine del suono e ancora sentirlo.Si applica solo a Sounds genitori a un BasePart o Attachment .
Come RollOffMaxDistance influisce sull'attenuazione di un suono (modo in cui scompare) dipende dalla ProprietàRollOffMode.
RollOffMinDistance
La distanza minima, in studs, a cui un Sound che è parentedato a un BasePart o Attachment inizierà a attenuarsi (riduzione del volume).
Come RollOffMinDistance influisce sull'attenuazione di un suono (modo in cui scompare) dipende dalla ProprietàRollOffMode.
RollOffMode
Questa proprietà controlla come il volume di un Sound che è parentedato a un BasePart o Attachment si attenua (scompare) man mano che diminuisce la distanza tra l'ascoltatore e il genitore.
Per i dettagli sui diversi modi, vedi Enum.RollOffMode .
SoundId
Questa proprietà è l'ID del contenuto del file audio da associare al Sound . Vedi Risorse audio per ulteriori informazioni.
TimeLength
La lunghezza del Sound in secondi. Se il Sound non viene caricato, questo valore sarà 0 .
Questa proprietà viene spesso utilizzata in combinazione con PlaybackSpeed per regolare la velocità di un suono in modo che duri per una durata specifica.
Campioni di codice
This code sample includes a simple function that uses Sound.TimeLength and Sound.PlaybackSpeed to play a sound that'll take the given duration to complete. It achieves this by setting the PlaybackSpeed of the sound to be equal to the TimeLength of the sound divided by the desired duration.
Note that as TimeLength is equal to 0 when the sound has not loaded, the function will yield while it loads the sound.
local function playForDuration(sound, duration)
if not sound.IsLoaded then
sound.Loaded:wait()
end
local speed = sound.TimeLength / duration
sound.PlaybackSpeed = speed
sound:Play()
end
local sound = script.Parent
playForDuration(sound, 5)
TimePosition
Questa proprietà riflette il progresso del Sound in secondi.Può essere cambiato per spostare la posizione di riproduzione del suono sia prima che durante la riproduzione.
Mentre viene riprodotto un Sound , TimePosition aumenta a un tasso di PlaybackSpeed per secondo.Una volta che TimePosition raggiunge TimeLength, il suono si fermerà a meno che non sia Looped.
Nota che impostare TimePosition a un valore superiore alla lunghezza in una traccia loopata non la farà avvolgere.Se questo comportamento è desiderato, considera il seguente snippet di codice:
local newPosition = 1.5if newPosition >= sound.TimeLength thennewPosition = newPosition - sound.TimeLengthendsound.TimePosition = newPosition
Campioni di codice
This sample demonstrates how Sound.TimePosition can be used to jump to particular points in an audio file both before and during Sound playback.
A Sound is created in the Workspace and set to start at 30 seconds in. During playback, it jumps forwards to 100 seconds and then back to the start (0 seconds).
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Parent = workspace
sound.TimePosition = 30
sound:Play()
task.wait(5)
sound.TimePosition = 100
task.wait(5)
sound.TimePosition = 0
Volume
Il volume del Sound . Può essere impostato tra 0 e 10 e predefinito su 0.5 .
Nota che se il Sound è un membro di un SoundGroup , il suo volume di riproduzione (ma non la sua ProprietàVolume ) sarà influenzato dalla Proprietàdel GruppoSoundGroup.Volume.
Metodi
Pause
Questo metodo interrompe il riproduzione del Sound se è in riproduzione, impostando Playing a false .A differenza di Stop() , non ripristina TimePosition , il che significa che il suono può essere ripreso utilizzando Resume() .
Restituzioni
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Play
Questo metodo gioca il Sound e imposta TimePosition all'ultimo valore impostato da uno script (o 0 se non è stato Impostare), quindi imposta Playing a true .
Restituzioni
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Resume
Questo metodo riprende il Sound e imposta Playing a true .Non altera TimePosition e quindi può essere utilizzato per riprendere il riproduzione di un suono interrotto attraverso Pause() .
Restituzioni
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Stop
Questo metodo interrompe il Sound e imposta Playing a false , quindi imposta TimePosition a 0 .
Restituzioni
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Eventi
DidLoop
Fuochi ogni volta che i Sound loop.Restituisce soundId e numOfTimesLooped , dando l'ID del contenuto del suono e il numero di volte ripetuto rispettivamente.
Quando il Sound viene interrotto attraverso Stop() , il contatore loopato viene ripristinato, il che significa che il prossimo evento DidLoop tornerà 1 per numOfTimesLooped.
Parametri
Campioni di codice
This code sample includes a function that will play a sound and allow it to loop for a given number of times before stopping it.
local function loopNTimes(sound, numberOfLoops)
if not sound.IsPlaying then
sound.Looped = true
local connection = nil
connection = sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print(numOfTimesLooped)
if numOfTimesLooped >= numberOfLoops then
-- disconnect the connection
connection:Disconnect()
-- stop the sound
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)
Ended
Si accende quando il Sound ha completato il riproduzione e si è fermato. Questo evento viene spesso utilizzato per distruggere un suono quando ha completato la riproduzione:
sound:Play()sound.Ended:Wait()sound:Destroy()
Nota che questo evento non non brucerà per i suoni con Looped impostati su true , poiché continueranno a suonare dopo aver raggiunto la Terminare.Questo evento non si attiverà anche non quando il suono viene interrotto prima che il riproduzione sia completata; per questo usare l'evento Stopped.
Parametri
Loaded
Si accende quando il Sound è caricato.
Poiché questo evento si attiva solo al momento in cui viene caricato il suono, è consigliato controllare la proprietà IsLoaded del suono prima di connettersi a questo evento.
Parametri
Campioni di codice
This simple function will verify a Sound has loaded by checking the Sound.IsLoaded property. If Sound.IsLoaded is false it will wait for the Loaded event before resuming.
It is important to check Sound.IsLoaded before connecting to the Sound.Loaded event, as if the sound has already loaded the Sound.Loaded event will not fire and the function will yield indefinitely.
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")
Paused
Si attiva ogni volta che il Sound viene interrotto utilizzando Pause() .
Parametri
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Played
Si attiva ogni volta che il Sound viene riprodotto utilizzando Play() .Questo evento non non brucerà se il Sound viene riprodotto a causa di PlayOnRemove che viene impostato su true e il suono viene distrutto
Parametri
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Resumed
Si accende quando il Sound viene ripreso utilizzando Resume() .
Parametri
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Stopped
Si accende quando il Sound viene interrotto utilizzando Stop() . Distruggere un suono mentre viene riprodotto non causerà l'Lanciaredi questo evento.
Parametri
Campioni di codice
This sample gives a simple demonstration of what each of the Sound functions (Sound.Play, Sound.Stop, Sound.Pause and Sound.Resume) do to Sound.Playing and Sound.TimePosition.
-- create a sound
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- listen for events
sound.Played:Connect(function(_soundId)
print("Sound.Played event")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused event")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed event")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped event")
end)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- play true 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- stop false 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0