Classe de moteur
Sound
*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.
Résumé
Propriétés
SoundId:ContentId |
Événements
DidLoop(soundId: string,numOfTimesLooped: number):RBXScriptSignal |
Ended(soundId: string):RBXScriptSignal |
Loaded(soundId: string):RBXScriptSignal |
Paused(soundId: string):RBXScriptSignal |
Played(soundId: string):RBXScriptSignal |
Resumed(soundId: string):RBXScriptSignal |
Stopped(soundId: string):RBXScriptSignal |
Échantillons de code
Part de Lecture de Musique
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
-- créer un son
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- diminuer la taille de l'émetteur (pour une diminution de volume antérieure)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- basculer le son entre lecture / non lecture
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)Référence API
Propriétés
EmitterSize
IsLoaded
Échantillons de code
Charger le son
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Le son a été chargé !")IsPaused
Échantillons de code
Son est en cours de lecture et Son est en pause
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) -- vrai, faux
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- faux, vrai
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- vrai, faux
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- faux, vraiIsPlaying
Échantillons de code
Son est en cours de lecture et Son est en pause
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) -- vrai, faux
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- faux, vrai
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- vrai, faux
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- faux, vraiisPlaying
Looped
Échantillons de code
Boucler un nombre de fois
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
-- déconnecter la connexion
connection:Disconnect()
-- arrêter le son
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)MaxDistance
MinDistance
Pitch
PlaybackLoudness
Échantillons de code
Barre d'Amplitude de Volume
-- à placer dans StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- attendre le PlayerGui du joueur local
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- créer un ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- créer un conteneur pour notre barre
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
-- créer une barre
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
-- créer un son
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- définir une amplitude maximale
local maxLoudness = 30
-- animer la barre d'amplitude
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackSpeed
Échantillons de code
Vitesse de lecture du son
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3x plus rapide
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2x plus lent
task.wait(5)
sound.PlaybackSpeed = 1 -- par défautPlaying
Échantillons de code
Lecture de son
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("lecture du son")
sound.Playing = true
task.wait(10)
print("arrêt du son")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("reprise à la position de temps : " .. tostring(timePosition)) -- env. 10 secondesSoundId
Sound.SoundId:ContentId
TimeLength
Échantillons de code
Jouer un son pendant une durée spécifique
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
Échantillons de code
Position de Temps du Son
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 = 0Méthodes
Pause
Sound:Pause():()
Retours
()
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0pause
Play
Sound:Play():()
Retours
()
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0play
Resume
Sound:Resume():()
Retours
()
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0Stop
Sound:Stop():()
Retours
()
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0stop
Événements
DidLoop
Échantillons de code
Boucler un nombre de fois
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
-- déconnecter la connexion
connection:Disconnect()
-- arrêter le son
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
Paramètres
Échantillons de code
Charger le son
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Le son a été chargé !")Paused
Paramètres
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0Played
Paramètres
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0Resumed
Paramètres
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0Stopped
Paramètres
Échantillons de code
Fonctions Sonores
-- créer un son
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- écouter les événements
sound.Played:Connect(function(_soundId)
print("Événement Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Événement Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Événement Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Événement Sound.Stopped")
end)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- pause faux 10
task.wait(3)
sound:Resume()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 10
task.wait(3)
sound:Stop()
print("arrêter", sound.Playing, sound.TimePosition) -- arrêter faux 0
task.wait(3)
sound:Play()
print("jouer", sound.Playing, sound.TimePosition) -- jouer vrai 0