Sound

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Un oggetto Sound è un oggetto che emette un suono.

Suono 2D e 3D

Un suono posizionato in un BasePart o in un Attachment emette il suo suono dalla posizione Class

Un suono è considerato "globale" se non è parented a BasePart o un Attachment . In questo caso, il suono sarà riprodotto 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.

Music Playing Part

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)

This sample gives a simple example of how a Sound that is not parented to a Part or Attachment will play at a constant volume throughout the place. A sound is instanced and parented to the Workspace, and then played.

Sound in the Workspace

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()

Sommario

Proprietà

  • Sola Lettura
    Non Replicato
    Non Navigabile
    Sicurezza Script Roblox
    Lettura Parallela
  • Sola Lettura
    Non Replicato
    Lettura Parallela

    Questa proprietà sarà vera quando il Sound è stato caricato dai server Roblox e è pronto per essere Giocare.

  • Nascosto
    Sola Lettura
    Non Replicato
    Lettura Parallela

    Questa proprietà read-only restituirà true quando il Sound non sta giocando.

  • Nascosto
    Sola Lettura
    Non Replicato
    Lettura Parallela

    Questa proprietà read-only restituirà true quando il Sound sta giocando.

  • Lettura Parallela

    Una gamma che denota l'inizio e la fine del ciclo all'interno della Sound.PlaybackRegion , in secondi.

  • Lettura Parallela

    Questo imposta se o no il Sound si ripetere una volta finito quando sta giocando.

  • Lettura Parallela

    Quando è vero, il Sound si attiverà quando sarà rimosso dal Gioco, affidandosi al genitore del Sound o uno dei suoi antenati per nulla.

  • Sola Lettura
    Non Replicato
    Lettura Parallela

    Un numero tra 0 e 1000 che indica quanto è rumoroso il Sound attualmente in Indietro.

  • Lettura Parallela

    Una gamma che denota l'inizio desiderato (min) e la fine (max) in un intervallo di tempo Sound.TimeLength , in secondi.

  • Lettura Parallela

    Se vero , questa proprietà dà il tuo accesso Sound alle proprietà Sound.PlaybackRegion e 1> Class.Sound.LoopRegion1>, che possono controllare più esattamente la sua riproduzione.

  • Non Replicato
    Lettura Parallela

    Determina la velocità con cui un Sound si Giocare, con valori più elevati che fanno si che il suono si esegua più velocemente e ad una nota più alta.

  • Non Replicato
    Lettura Parallela

    Indica se il Sound sta attualmente giocando.

  • Lettura Parallela

    La distanza massima, in studs, che un client di ascolto può essere dall'origine Sound\s e ascoltarlo ancora. Si applica solo ai suoni legati a un Part o Attachment (suoni 3D).

  • Lettura Parallela

    La distanza minima, in studs, a cui un 3D Sound (figlio diretto di un BasePart o Attachment ) inizierà a diminuire (diminuire in volume).

  • Lettura Parallela

    Controlla il modo in cui il volume di un 3D Sound (genitore di un BasePart o Attachment ) si comporta come la distanza tra il lettore e il genitore del suono.

  • Lettura Parallela

    Il SoundGroup che è collegato a questo Sound . I Volume e SoundEffects applicati a questo gruppo sonoro saranno trasmessi al suono. Un suono può essere in un solo gruppo sonoro alla volta.

  • SoundId:ContentId
    Lettura Parallela

    Questa proprietà è l'ID del contenuto del file audio a cui un oggetto Sound è associato. Una volta caricato il suono, l'ID del contenuto può essere trovato nell'URL del suono caricato.

  • Sola Lettura
    Non Replicato
    Lettura Parallela

    La lunghezza del Sound in secondi. Se il Sound non è caricato, questo valore sarà 0.

  • Non Replicato
    Lettura Parallela

    Mostra il progresso in secondi del Sound . Può essere cambiato per spostare la posizione di riproduzione del Sound sia prima che durante la riproduzione.

  • Lettura Parallela

    Il volume della Sound . Può essere impostato tra 0 e 10. Predefiniti a 0.5.

Metodi

Eventi

Proprietà

ChannelCount

Sola Lettura
Non Replicato
Non Navigabile
Sicurezza Script Roblox
Lettura Parallela

IsLoaded

Sola Lettura
Non Replicato
Lettura Parallela

Questa proprietà sarà vera quando il Sound è stato caricato dai server Roblox e è pronto per essere Giocare.

In Roblox, i file audio non vengono memorizzati nei giochi stessi, ma vengono hostati nei server Roblox e referenziati dalla ProprietàSound.SoundId. Ciò significa che devono essere scaricati sul dispositivo del client prima di poter essere riprodotti. Ciò può richiedere un po 'di tempo a seconda della connessione Internet dell'utente, della lunghezza del suono e del numero di altri oggetti che devono essere caricati.

Gli sviluppatori possono utilizzare la proprietà Sound.IsLoaded e l'evento Sound.Loaded se desiderano verificare se un suono è stato caricato prima di giocarlo.

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.

Load Sound

local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("The sound has loaded!")

IsPaused

Nascosto
Sola Lettura
Non Replicato
Lettura Parallela

Questa proprietà read-only restituirà true quando il Sound non sta giocando. Nota che questa proprietà non restituirà true solo una volta che un suono è stato暂停 utilizzando la funzione Sound:Pause() ma anche se non è stato giocato utilizzando la funzione Sound:Stop() o mai giocato.

Questa proprietà sarà vera solo quando Sound.IsPlaying è falso.

Come IsPaused è letto solo, non può essere usato per fermare il suono, Sound:Stop() e Sound:Pause() dovrebbe essere usato 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.

Sound IsPlaying and SoundIsPaused

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

Nascosto
Sola Lettura
Non Replicato
Lettura Parallela

Questa proprietà read-only restituirà true quando il Sound sta giocando.

Questa proprietà può essere vera solo quando Sound.IsPaused è falsa.

Poiché IsPlaying è in lettura solo, non può essere utilizzato per riprodurre il suono, Sound: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.

Sound IsPlaying and SoundIsPaused

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

Lettura Parallela

Un intervallo che denota l'inizio e la fine di un ciclo all'interno della Sound Sound.PlaybackRegion in secondi.

Looped

Lettura Parallela

Questo imposta se o no il Sound si ripetere una volta finito quando sta giocando.

I suoni a loop sono adatti a una gamma di applicazioni tra cui musica e suoni ambientali di sottofondo. L'evento Sound.DidLoop può essere utilizzato per tracciare il numero di volte in cui il suono è stato looped.

Campioni di codice

Whilst looping is most appropriate for longer tracks such as music or ambient sounds, this sample demonstrates sound looping by creating the Roblox death sound in every character's head and looping it.

The Players.PlayerAdded and Player.CharacterAdded functions are used to detect new player characters. One a new character has been added a sound is created in their head. For demonstration purposes, the Sound.DidLoop function is used to show how the number of times a particular sound has looped can be tracked.

Sound Looping

local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onCharacterAdded(character)
-- wait for the head to be added
local head = character:WaitForChild("Head")
local sound = Instance.new("Sound")
sound.Name = "TestSound"
sound.SoundId = "rbxasset://sounds/uuhhh.mp3" -- oof
sound.Parent = head
sound.Looped = true
sound.DidLoop:Connect(function(_soundId, numOfTimesLooped)
print("oof! " .. tostring(numOfTimesLooped))
end)
sound:Play()
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)

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.

Loop a Number of Times

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

Lettura Parallela

Quando è vero, il Sound si attiverà quando verrà rimosso dal Gioco.

Nota che il suono si attiverà quando la proprietà Instance.Parent di Class.Sound o uno dei suoi antenati è impostato su null. Ciò significa che tutto il seguente causerà il suono quando PlayOnRemove è vero. Nota, questo include Sound come la funzione di distruzione imposta il padre su null.

suono: distruggi il suono.Parent = nil suono.Parent.Parent = nil

Campioni di codice

In this sample a sound is created in the workspace, PlayOnRemove is set to true and the sound is then destroyed.

As Sound.PlayOnRemove is true, the sound will play when it is removed.

Sound PlayOnRemove

local sound = Instance.new("Sound")
sound.Name = "TestSound"
sound.SoundId = "rbxasset://sounds/uuhhh.mp3" -- oof
sound.Parent = workspace
sound.PlayOnRemove = true
task.wait(3)
sound:Destroy()

PlaybackLoudness

Sola Lettura
Non Replicato
Lettura Parallela

Un numero tra 0 e 1000 che indica quanto è rumoroso il Sound attualmente in Indietro.

Questa proprietà riflette l'ampitudine della riproduzione del suono nell'istanza del tempo in cui viene letto. Quindi, per la maggior parte dei suoni, si alternerà costantemente. A causa di questo, può apparire nella finestra delle proprietà Roblox Studio come 0, tuttavia quando viene letto dal codice nella barra di comando o Scripts restituirà il valore corretto.

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.

Volume Amplitude Bar

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

Lettura Parallela

Una gamma che denota l'inizio desiderato (min) e la fine (max) in un intervallo di tempo Sound.TimeLength , in secondi.

  • Se PlaybackRegion.Min > 0, il suono inizia a riprodursi dal PlaybackRegion.Min time.

  • Se PlaybackRegion.Min < 0, il suono inizia a suonare da 0.

  • Se PlaybackRegion.Max > Sound.TimeLength , il suono si ferma a Sound.TimeLength .

  • Se PlaybackRegion.Max < Sound.TimeLength , il suono si ferma a esattamente quel momento.

  • Se PlaybackRegion.Min == PlaybackRegion.Max, la PlayBackRegion è Inattivo, Inattiva, Inattivi.

PlaybackRegionsEnabled

Lettura Parallela

Se vero , questa proprietà dà il tuo accesso Sound alle proprietà Sound.PlaybackRegion e 1> Class.Sound.LoopRegion1>, che possono controllare più esattamente la sua riproduzione.

PlaybackSpeed

Non Replicato
Lettura Parallela

Determina la velocità con cui un Sound riproduce il Giocare. Più grande è il valore, più veloce è il suono che Indietro.

Ad esempio, un valore di 2 farà in modo che il Sound suoni 2x più velocemente, mentre un valore di 0.5 lo farà suonare 2x più lentamente. Quando PlaybackSpeed è pari a 1, il suono richiederà Sound.TimeLength (in secondi) per completarsi.

Nota che l'aumento della velocità di riproduzione di un suono lo farà giocare a un pitch più alto.

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.

Sound PlaybackSpeed

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

Non Replicato
Lettura Parallela

Indica se il Sound sta attualmente giocando. Questo può essere attivato e questa proprietà si replica sempre.

Nell'editor dello studio, Sounds non sono giocabili poiché il tempo è fermato. Impostando Sound.Playing in modo veritiero in modalità modifica non si ottiene nulla. Tuttavia, puoi giocare l'audio da dentro dei plugin impostando il Sounds come discendenti di un 1> Class.PluginGui1>

Questa proprietà non dovrebbe essere confusa con Sound.IsPlaying che è una Proprietàdi lettura. Il giocare può essere impostato su true o false per avviare o interrompere il playback di un suono.

Nota che quando Play è impostato su false, la proprietà Sound.TimePosition del suono non si Ripristina. Ciò significa che quando Play è impostato su true again l'audio continuerà dalla posizione temporale in cui è stato interrotto. Tuttavia, se la funzione Sound:Play() viene utilizzata per riassumere il suono, la posizione temporale sarà ripristata a 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.

Sound Playing

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

Lettura Parallela

La distanza massima, in studs, di un client's listener può essere dall'origine Sound e comunque ascoltarla. Si applica solo ai suoni legati a un Part o Attachment (suoni 3D).

Come RollOffMaxDistance influisce sulla riduzione di un suono (maniera in cui sbiadisce) è dipendente dalla ProprietàSound.RollOffMode. Quando RollOffMode è impostato per utilizzare un modello di distanza invertito (Inverse o InverseTapered) il RollOffMaxDistance non influisce sulla riduzione del suono. Ciò significa

Quando RollOffMode è impostato su un modello di distanza lineare (Linear o LinearSquared) il suono si attenua tra Sound.EmitterSize e MaxDistance (con volume di riproduzione che raggiunge zero a RollOffMaxDistance). Ciò è meno realistico, ma in alcuni casi consente di gestire l'attenuazione in modo più intuitivo.

RollOffMinDistance

Lettura Parallela

La distanza minima, in studs, a cui un 3D Sound (figlio diretto di un BasePart o Attachment ) inizierà a diminuire (diminuire in volume).

Sembra essere legato a un BasePart o Attachment che sono discendenti di Workspace sono considerati suoni 3D e il loro volume mentre si gioca dipende dalla distanza tra il suono del cliente ( 1>

Il modo in cui il Sound si attenua (disattiene) dopo che la distanza tra il lettore e il suono supera la distanza di RollOffMinDistance è determinata dal RollOffMode.

RollOffMode

Lettura Parallela

Questa proprietà imposta il modo in cui 3D Sounds attenuate (fade out) come la distanza tra il lettore e il padre del suono aumenta. Può essere impostato su uno dei valori dell'Enum.RollOffMode枚.

Il seguente codice imposterà RollOffMode su Linear:


sound.RollOffMode = Enum.RollOffMode.Linear

I diversi modelli

Le seguenti opzioni sono disponibili:


<tbody>
<tr>
<td>Inverso</td>
<td>Il volume si attenua da <code>Sound/RollOffMinDistance</code> in modo inversato.</td>
</tr>
<tr>
<td>InverseTapered</td>
<td>Un modello ibrido. Segue il modello Inverse quando vicino a <code>RollOffMinDistance</code> e il modello Linear Square quando vicino a <code>Sound/RollOffMaxDistance</code> .</td>
</tr>
<tr>
<td>Lineare</td>
<td>Il volume si attenua tra <code>RollOffMinDistance</code> e <code>Sound/RollOffMaxDistance</code> con una relazione lineare.</td>
</tr>
<tr>
<td>LineaQuadrata</td>
<td>Il volume si attenua tra <code>RollOffMinDistance</code> e <code>Sound/RollOffMaxDistance</code> con una relazione lineare quadrata.</td>
</tr>
</tbody>
ModalitàDescrizione

Inversione della distanza inversa vs lineare

Per impostazione predefinita, i suoni sono impostati per utilizzare l'attenuazione della distanza inversa (Enum.RollOffMode.Inverse) che riflette il modo in cui i suoni si attenuano nel Mondoreale. Sotto l'attenuazione della distanza inversa, i suoni inizieranno ad attenuarsi una volta che la distanza tra il lettore e

RollOffMaxDistance non influisce sulla distorsione sotto il modello invertito, ma farà si che il suono si tagli completamente una volta raggiunta questa distanza. Questo può essere particolarmente improvvisato quando si utilizzano valori bassi per la distanza massima.

La distanza lineare attenuata funziona in modo diverso. Under linear distance attenuation il suono attenuirà tra RollOffMinDistance e RollOffMaxDistance, facendo silenzioso una volta raggiunto MaxDistance. RollOffMinDistance mostra ancora il punto in cui il suono inizierà ad attenuarsi. Tuttavia, il volume audibile in qualsiasi punto ora dipende dal punto in cui il ricev

SoundGroup

Lettura Parallela

Il SoundGroup che è linkato a questo Sound . SoundGroup.Volume e 1> Class.SoundEffect|SoundEffects1> applicato a questo gruppo sonoro passa al suono. Un suono può essere in un solo gruppo sonoro alla volta.

SoundGroups vengono utilizzati per gestire il volume e gli effetti di più Sounds contemporaneamente. A Sound viene aggiunto a un 0> Class.SoundGroup0> impostando la proprietà SoundGroup della sound.

Campioni di codice

This sample demonstrates how a SoundGroup can be used to change the volume of its associated Sounds and apply SoundEffects.

In this example a Sound is instanced in the Workspace and assigned to a new SoundGroup. The Sound is played and during playback the volume is changed via the SoundGroup and a SoundEffect is added.

SoundGroups

local SoundService = game:GetService("SoundService")
-- create a sound group
local soundGroup = Instance.new("SoundGroup")
soundGroup.Parent = SoundService
-- create a sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.PlaybackSpeed = 2
sound.SoundGroup = soundGroup
sound.Parent = workspace
-- play the sound
sound:Play()
task.wait(10)
-- change the volume
soundGroup.Volume = 0.1
task.wait(3)
-- return the volume
soundGroup.Volume = 0.5
task.wait(4)
-- add a sound effect
local reverb = Instance.new("ReverbSoundEffect")
reverb.Parent = soundGroup

SoundId

ContentId
Lettura Parallela

Questa proprietà è l'ID del contenuto del file audio a cui un oggetto Sound è associato. Una volta caricato il suono, l'ID del contenuto può essere trovato nell'URL del suono caricato.

È importante ricordare che l'URL non è lo stesso dell'ID del contenuto. Funzionerà quando viene pastato direttamente nella proprietà SoundId di un Sound in Roblox studio, poiché Studio lo correggerà automaticamente, tuttavia se viene impostato da un Script allora l'ID del contenuto corretto sarà necessario, utilizzando il numero dall'URL. Ad esempio:


"https://www.roblox.com/catalog/9120386436" -- URL del web (non funzionerà)
"http://www.roblox.com/asset/?id=9120386436" -- ID del contenuto (funzionerà)
"rbxassetid://9120386436" -- Content ID (alternative version, will work)

Campioni di codice

This sample gives a simple example of how a Sound that is not parented to a Part or Attachment will play at a constant volume throughout the place. A sound is instanced and parented to the Workspace, and then played.

Sound in the Workspace

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()

TimeLength

Sola Lettura
Non Replicato
Lettura Parallela

La lunghezza del Sound in secondi. Se il Sound non è caricato, questo valore sarà 0.

Questa proprietà viene spesso utilizzata in conjunction con Sound.PlaybackSpeed per regolare la velocità di un suono in modo che dura per un periodo di tempo specifico (vedi esempi). Quando Sound.PlaybackSpeed è uguale a 1, il suono richiede TimeLength secondi per completarsi.

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.

Play a Sound for a Specific Duration

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

Non Replicato
Lettura Parallela

Mostra il progresso in secondi del Sound . Può essere cambiato per spostare la posizione di riproduzione del suono. Se il suono sta già riproducendo, allora la riproduzione si attaccherà alla posizione specificata. Se non sta riproducendo il Sound inizierà la riproduzione al posizione specificata quando il suono viene riprodotto.

Mentre viene riprodotto un Sound, TimePosition aumenta in modo lineare di Sound.PlaybackSpeed per secondo. Una volta che TimePosition raggiunge Sound.TimeLength , il suono si ferma a meno che non sia impostato su 2>Class.Sound.Looped2> in modo che

Nota che impostare TimePosition su un valore maggiore della lunghezza in una pista looped non causerà che si avvolga intorno. Se tale comportamento è desiderato, il sviluppatore dovrebbe fare quanto Seguendo.

Se newPosition >= sound.TimeLength, allora newPosition = newPosition - sound.TimeLength, posizione di fine suono.Position = newPosition

Impostare TimePosition a un valore inferiore a zero non influisce sull'esecuzione, ma questo comportamento non dovrebbe essere affidato.

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

Sound TimePosition

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

Lettura Parallela

Il volume della Sound . Può essere impostato tra 0 e 10. Predefiniti a 0.5

Nota che se il Sound è un membro di un SoundGroup il suo volume di riproduzione (ma non la ProprietàVolume) sarà influenzato dalla proprietà SoundGroup.Volume di quel 2>Class.SoundGroup2> . L'effetto di questo è multi

Campioni di codice

This sample includes a demonstration of how a Sound's volume can be set using the Sound.Volume property.

Sound Volume

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.PlaybackSpeed = 2
sound.Parent = workspace
sound.Volume = 2
sound:Play()
task.wait(7)
sound.Volume = 0.2

Metodi

Pause

void

Imposta Sound.Playing su false. Ciò sospende il playback del Sound se il suono è in esecuzione. A differenza di Sound:Stop() , non ripristina 1> Class.Sound.TimePosition1> , il che significa che il suono può essere ripreso utilizzando 4> Class.Sound:Resume()</

L'impatto delle diverse funzioni di suono su Sound.Playing e Sound.TimePosition sono mostrati di seguito.


<tbody>
<tr>
<td>Suono:Play()</td>
<td>Ver</td>
<td>Ultimo valore impostato in Lua (valore predefinito 0)</td>
</tr>
<tr>
<td>Suono:Pausa()</td>
<td>Falso</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono: Resume()</td>
<td>Ver</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono:Stop()</td>
<td>Falso</td>
<td>0</td>
</tr>
</tbody>
FunzioneSuono.GiocandoPosizione temporale del suono

Restituzioni

void

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.

Sound Functions

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

void

Riproduce il Sound . Imposta Sound.TimePosition su l'ultimo valore impostato da un Script (o 0 se non è stato Impostare), quindi imposta 1> Class.Sound.Playing1> su true.

L'impatto delle diverse funzioni Sound su Sound.Playing e Sound.TimePosition sono mostrati di seguito.


<tbody>
<tr>
<td>Suono:Play()</td>
<td>Ver</td>
<td>Ultimo valore impostato in Lua (valore predefinito 0)</td>
</tr>
<tr>
<td>Suono:Pausa()</td>
<td>Falso</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono: Resume()</td>
<td>Ver</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono:Stop()</td>
<td>Falso</td>
<td>0</td>
</tr>
</tbody>
FunzioneSuono.GiocandoPosizione temporale del suono

Restituzioni

void

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.

Sound Functions

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

void

Ripristina il Sound . Imposta Sound.Playing su verità. Non modifica Sound.TimePosition e quindi può essere utilizzato per riavviare il playback di un suono interrotto utilizzando 1> Class.Sound:Pause()1> .

L'impatto delle diverse funzioni audio su Sound.Playing e Sound.TimePosition sono mostrati di seguito.


<tbody>
<tr>
<td>Suono:Play()</td>
<td>Ver</td>
<td>Ultimo valore impostato in Lua (valore predefinito 0)</td>
</tr>
<tr>
<td>Suono:Pausa()</td>
<td>Falso</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono: Resume()</td>
<td>Ver</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono:Stop()</td>
<td>Falso</td>
<td>0</td>
</tr>
</tbody>
FunzioneSuono.GiocandoPosizione temporale del suono

Restituzioni

void

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.

Sound Functions

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

void

Interrompe il Sound . Imposta Sound.Playing su false, quindi imposta Sound.TimePosition su 0.

L'impatto delle diverse funzioni audio su Sound.Playing e Sound.TimePosition sono mostrati di seguito.


<tbody>
<tr>
<td>Suono:Play()</td>
<td>Ver</td>
<td>Ultimo valore impostato in Lua (valore predefinito 0)</td>
</tr>
<tr>
<td>Suono:Pausa()</td>
<td>Falso</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono: Resume()</td>
<td>Ver</td>
<td>italian:</td>
</tr>
<tr>
<td>Suono:Stop()</td>
<td>Falso</td>
<td>0</td>
</tr>
</tbody>
FunzioneSuono.GiocandoPosizione temporale del suono

Restituzioni

void

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.

Sound Functions

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

Evento che si attiva ogni volta che si esegue il ciclo Sound. Ritorna soundId e numOfTimesLooped, fornendo l'ID contenuto del suono e il numero di volte looped rispettivamente.

Quando il Sound è fermato, il contatore looped resets meaning il prossimo evento DidLoop restituisce 1 for numOfTimesLooped.

Parametri

soundId: string

Il Sound.SoundId della Sound che si è ripetuta.

numOfTimesLooped: number

Il numero di volte che il Sound ha fatto la looping.


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.

Loop a Number of Times

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 attiva quando il Sound ha completato il playback e si è fermato. Nota che questo evento non si attiva per i suoni con Sound.Looped impostato su true mentre continuano a giocare quando raggiungono la Terminare.

Questo evento viene spesso utilizzato per distruggere un suono quando ha completato il playback.


sound:Play()
sound.Ended:Wait()
sound:Destroy()

Questo evento si attiva solo se il suono ha raggiunto la sua Terminare. Ciò significa che non si attiverà anche quando il suono sarà interrotto prima che il playback sia completato, per questo motivo Sound.Stopped .

Parametri

soundId: string

Il Sound.SoundId della Sound che è finita.


Loaded

L'evento Sound.Loaded si attiva quando il Sound è caricato.

Nota che questo evento si attiverà solo quando il suono è caricato. Ciò significa che se viene ascoltato quando il suono è già caricato non Riportare. Quindi è consigliato controllare Sound.IsLoaded prima di connettersi a questo evento.

Parametri

soundId: string

Il Sound.SoundId della Sound che si è caricata.


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.

Load Sound

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 Sound è sospeso utilizzando Sound:Pause() .

Come con Sound.Played , Sound.Resumed e Sound.Stopped , solo la funzione sonora rispettiva causerà l'evento di Lanciare. Ciò significa che Pause non si attiverà mai quando 1> Class.Sound:Pause()1> è chiamato.

Parametri

soundId: string

Il Sound.SoundId della Sound che è stata暂停.


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.

Sound Functions

-- 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 viene riprodotto il Sound utilizzando la funzione Sound:Play() .

Come con Sound.Stopped , Sound.Paused e Sound.Resumed , solo la funzione sonora rispettiva causerà l'evento di Lanciare. Ciò significa che Played non causerà il fuoco

Parametri

soundId: string

Il Sound.SoundId della Sound che è stata riprodotta.


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.

Sound Functions

-- 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 attiva quando il Sound è ripreso utilizzando Sound:Resume() .

Come con Sound.Played , Sound.Paused e Sound.Stopped , solo la funzione audio rispettiva causerà l'evento di Lanciare. Ciò significa che Resume funzionerà solo quando 1> Class.Sound:Resume()1> è chiamato.

Parametri

soundId: string

Il Sound.SoundId della Sound in ripresa.


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.

Sound Functions

-- 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 attiva quando il Sound è interrotto a causa della funzione Sound:Stop() .

Come con Sound.Played , Sound.Paused e Sound.Resumed , solo la funzione sonora rispettiva causerà l'evento di Lanciare. Ciò significa che Stopped non causerà il fuoco quando 1> Class.Sound:Stop()1> è chiamato. Distruendo un suono mentre si esegue non causerà

Parametri

soundId: string

Il Sound.SoundId della Sound che si è fermata.


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.

Sound Functions

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