Sound
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
Sound เป็นวัตถุที่ออกเสียงเมื่อวางใน BasePart หรือ Attachment วัตถุนี้จะปล่อยเสียงจากส่วน BasePart.Position หรือแนบอนุกรม Attachment.WorldPosition ของมันในการจัดวางนี้ Sound แสดงผลกระทบของ Doppler ซึ่งหมายความว่าความถี่และระดับสูงของมันจะแตกต่างกันไปตามการเคลื่อนไหวแบบสัมพันธ์ของส่วนหรือชิ้นส่วนที่มันติดอยู่นอกจากนี้ปริมาณของมันจะถูกกำหนดโดยระยะห่างระหว่างผู้ฟังเสียงของไคลเอนต์ (โดยปกติคือตำแหน่ง Camera ) และตำแหน่งของพ่อของเสียงสำหรับข้อมูลเพิ่มเติม ดู RollOffMode
เสียงจะถูกมองว่าเป็น "ทั่วถึง" หากมันไม่ได้เป็น ไม่ ถูกผูกกับ BasePart หรือ Attachmentในกรณีนี้เสียงจะเล่นด้วยระดับเดียวกันทั่วทั้งสถานที่
ตัวอย่างโค้ด
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)
สรุป
คุณสมบัติ
คุณสมบัตินี้คือ true เมื่อ Sound โหลดจากเซิร์ฟเวอร์ Roblox และพร้อมที่จะเล่น
คุณสมบัติอ่านเฉพาะที่ส่งคืน true เมื่อ Sound ไม่ได้เล่น
คุณสมบัติอ่านเฉพาะที่ส่งคืน true เมื่อ Sound กำลังเล่น
ช่วงที่ระบุว่าเป็นจุดเริ่มต้นและจุดสิ้นสุดของลูปที่ต้องการภายใน PlaybackRegion ในเวลาไม่กี่วินาที
ตั้งค่าว่าจะซ้ำหรือไม่ซ้ำ Sound เมื่อเล่นเสร็จ
เมื่อ true , Sound จะเล่นเมื่อถูกลบออกจากประสบการณ์
ตัวเลขระหว่าง 0 และ 1000 บ่งบอกถึงความดังที่ Sound กําลังเล่นอยู่ในปัจจุบัน
ช่วงที่ระบุเวลาเริ่มต้นและหยุดที่ต้องการภายใน TimeLength ในวินาที
หาก true , คุณสมบัตินี้ให้การเข้าถึง Sound ของคุณกับ PlaybackRegion และ LoopRegion คุณสมบัติที่สามารถควบคุมการเล่นได้อย่างแม่นยํามากขึ้น
กำหนดความเร็วที่ Sound จะเล่นด้วยค่าที่สูงกว่าทำให้เสียงเล่นเร็วขึ้นและในระดับที่สูงขึ้น
บ่งบอกว่า Sound กําลังเล่นอยู่ในขณะนี้
ระยะสูงสุดในสตัดที่ผู้ฟังของไคลเอนต์สามารถอยู่จากจุดกำเนิดของเสียงและยังได้ยินมันได้ใช้ได้เฉพาะกับ Sounds ผู้ปกครองที่มีลูก BasePart หรือ Attachment
ระยะขั้นต่ำในสตัดที่ซึ่ง Sound ซึ่งเป็นบิดาของ BasePart หรือ Attachment จะเริ่มลดลง (ลดปริมาณ)
ควบคุมวิธีที่ปริมาณของ Sound ซึ่งได้รับการผูกมัดกับ BasePart หรือ Attachment ลดลง (จางไป) ตามระยะห่างระหว่างผู้ฟังและผู้ปกครองที่เปลี่ยนแปลง
The SoundGroup ที่เชื่อมโยงกับสิ่งนี้ Sound .
ID เนื้อหาของไฟล์เสียงที่จะเชื่อมโยงกับ Sound
ความยาวของ Sound ในวินาที
ความคืบหน้าของ Sound ในวินาที สามารถเปลี่ยนเพื่อเคลื่อนย้ายตำแหน่งการเล่นของ Sound ทั้งก่อนและระหว่างการเล่น
ปริมาณของ Sound .
วิธีการ
คุณสมบัติ
ChannelCount
IsLoaded
คุณสมบัตินี้คือ true เมื่อ Sound โหลดจากเซิร์ฟเวอร์ Roblox และพร้อมที่จะเล่นคุณสามารถใช้คุณสมบัตินี้และเหตุการณ์ Loaded เพื่อตรวจสอบว่าเสียงได้โหลดแล้วก่อนเล่น
ตัวอย่างโค้ด
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
คุณสมบัติอ่านเฉพาะนี้จะคืน true เมื่อ Sound ไม่ได้เล่นโปรดทราบว่ามันสามารถส่งคืน true หากเสียงถูกหยุดโดยใช้ Pause() หากถูกหยุดโดยใช้ Stop() หรือเสียงไม่เคยถูกเล่น
เนื่องจาก IsPaused เป็นแบบอ่านเฉพาะ ไม่สามารถใช้เพื่อหยุดเสียงได้; Stop() หรือ Pause() ควรใช้แทน
ตัวอย่างโค้ด
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
คุณสมบัติอ่านเฉพาะนี้จะส่งคืนความจริงเมื่อ Sound กำลังเล่น
เนื่องจาก IsPlaying เป็นแบบอ่านได้เท่านั้นจึงไม่สามารถใช้เพื่อเล่นเสียงได้; Play() ควรใช้แทน
ตัวอย่างโค้ด
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
ช่วงที่ระบุว่าเป็นจุดเริ่มต้นและจุดสิ้นสุดของลูปที่ต้องการภายใน PlaybackRegion ในเวลาไม่กี่วินาที
หาก Class.Sound.LoopRegion|LoopRegion.Min``>``Class.Sound.PlaybackRegion|PlaybackRegion.Min , ลูปจะเริ่มจาก LoopRegion.Min .
หาก Class.Sound.LoopRegion|LoopRegion.Min``<``Class.Sound.PlaybackRegion|PlaybackRegion.Min , ลูปจะเริ่มจาก PlaybackRegion.Min .
หาก Class.Sound.LoopRegion|LoopRegion.Max``>``Class.Sound.PlaybackRegion|PlaybackRegion.Max , ลูปจะเริ่มที่ PlaybackRegion.Max .
หาก , ลูปจะเริ่มต้นที่ ในเวลานั้นอย่างแน่นอน
หาก Class.Sound.LoopRegion|LoopRegion.Min``==``Class.Sound.LoopRegion|LoopRegion.Max , คุณสมบัติ Sound จะใช้แทนที่คุณสมบัติ PlaybackRegion
Looped
สิ่งนี้กำหนดว่าจะมีการทำซ้ำ Sound หรือไม่เมื่อเล่นเสร็จแล้วเสียงซ้ำๆ เหมาะสำหรับการใช้งานหลากหลายรวมถึงเพลงและเสียงสิ่งแวดล้อมพื้นหลัง
อีเวนต์ DidLoop สามารถใช้เพื่อติดตามจํานวนครั้งที่เสียงถูกซ้ำไป
ตัวอย่างโค้ด
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
เมื่อ true , the Sound จะเล่นเมื่อถูกลบออกจากประสบการณ์โดยการผูกมัด Sound หรือหนึ่งหากบรรพบุรุษของมันไปยัง nilซึ่งหมายความว่าทั้งหมดต่อไปนี้จะทำให้เสียงเล่นเมื่อ PlayOnRemove เป็น true :
- sound:Destroy()
- sound.Parent = nil
- sound.Parent.Parent = nil
PlaybackLoudness
ตัวเลขระหว่าง 0 และ 1000 บ่งบอกถึงความดังที่ Sound กําลังเล่นอยู่ในปัจจุบันคุณสมบัตินี้สะท้อนถึงความถี่ของการเล่นเสียงในตัวอย่างเวลาที่อ่านได้
ตัวอย่างโค้ด
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
ช่วงที่ระบุเวลาเริ่มต้นและหยุดที่ต้องการภายใน TimeLength ในวินาที
หาก PlaybackRegion.Min``>``0 , เสียงจะเริ่มเล่นจากเวลา PlaybackRegion.Min
หาก PlaybackRegion.Min``<``0 เสียงจะเริ่มเล่นจาก 0
หาก PlaybackRegion.Max``>``Class.Sound.TimeLength เสียงจะหยุดที่ Sound.TimeLength
หาก เสียงจะหยุดที่ ในเวลานั้นอย่างแน่นอน
หาก Class.Sound.PlaybackRegion|PlaybackRegion.Min``==``Class.Sound.PlaybackRegion|PlaybackRegion.Max ไม่ได้ใช้งานอยู่
PlaybackRegionsEnabled
หาก true , คุณสมบัตินี้ให้การเข้าถึง Sound ของคุณกับ PlaybackRegion และ LoopRegion คุณสมบัติที่สามารถควบคุมการเล่นได้อย่างแม่นยํามากขึ้น
PlaybackSpeed
กำหนดความเร็วที่ Sound จะเล่นด้วยค่าที่สูงกว่าทำให้เสียงเล่นเร็วขึ้นและในระดับที่สูงขึ้น
ตัวอย่างโค้ด
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
บ่งบอกว่า Sound กําลังเล่นอยู่ในขณะนี้ สิ่งนี้สามารถสลับได้และคุณสมบัตินี้จะสําเร็จซ้ําเสมอ
ในหน้าต่าง คุณสมบัติ ของ Studio ในขณะที่อยู่ในโหมด แก้ไข สลับไปที่ Playing เพื่อ true จะไม่เริ่มเล่นเสียง แต่เสียงจะเริ่มเล่นในระหว่างการทำงาน
คุณสมบัตินี้ไม่ควรสับสนกับ IsPlaying ซึ่งเป็นคุณสมบัติอ่านเฉพาะ
โปรดทราบว่าเมื่อ Playing ถูกตั้งค่าเป็น false คุณสมบัติ TimePosition ของเสียงจะไม่รีเซ็ต ซึ่งหมายความว่าเมื่อ Playing ถูกตั้งค่าเป็น true อีกครั้ง เสียงจะยังคงดำเนินต่อจากตำแหน่งเวลาที่หยุดเมื่อถูกหยุดอย่างไรก็ตาม หากฟังก์ชัน Play() ถูกใช้เพื่อดำเนินการต่อเสียง ตำแหน่งเวลาจะรีเซ็ตเป็น 0
ตัวอย่างโค้ด
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
ระยะสูงสุดในสตัดที่ผู้ฟังของไคลเอนต์สามารถอยู่จากจุดกำเนิดของเสียงและยังได้ยินมันได้ใช้ได้เฉพาะกับ Sounds ผู้ปกครองที่มีลูก BasePart หรือ Attachment
วิธีที่ RollOffMaxDistance ส่งผลต่อการลดเสียง (วิธีที่มันจะจางหายไป) ขึ้นอยู่กับคุณสมบัติ RollOffMode
RollOffMinDistance
ระยะขั้นต่ำในสตัดที่ซึ่ง Sound ซึ่งเป็นบิดาของ BasePart หรือ Attachment จะเริ่มลดลง (ลดปริมาณ)
วิธีที่ RollOffMinDistance ส่งผลต่อการลดเสียง (วิธีที่มันจะจางหายไป) ขึ้นอยู่กับคุณสมบัติ RollOffMode
RollOffMode
คุณสมบัตินี้ควบคุมวิธีที่ปริมาณของ Sound ซึ่งได้รับการผูกมัดกับ BasePart หรือ Attachment ลดลง (จางไป) ตามระยะห่างระหว่างผู้ฟังและผู้ปกครองเปลี่ยนแปลง
สำหรับรายละเอียดเกี่ยวกับโหมดต่างๆ ดูที่ Enum.RollOffMode
SoundId
คุณสมบัตินี้เป็นรหัสเนื้อหาของไฟล์เสียงที่จะเชื่อมโยงกับ Sound ดู สื่อเสียง สำหรับข้อมูลเพิ่มเติม
TimeLength
ความยาวของ Sound ในวินาที หาก Sound ไม่โหลด ค่านี้จะเป็น 0
คุณสมบัตินี้มักใช้ร่วมกับ PlaybackSpeed เพื่อปรับความเร็วของเสียงให้อยู่ในระยะเวลาที่กำหนด
ตัวอย่างโค้ด
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
คุณสมบัตินี้สะท้อนความคืบหน้าของ Sound ในเวลาแปดวินาทีสามารถเปลี่ยนเพื่อเลื่อนตำแหน่งการเล่นของเสียงทั้งก่อนและระหว่างการเล่น
เมื่อเล่นเป็น จะเพิ่มขึ้นในอัตรา ต่อวินาทีเมื่อ TimePosition ถึง TimeLength เสียงจะหยุดถ้าไม่ใช่ Looped
โปรดทราบว่าการตั้งค่า TimePosition เป็นค่าที่มากกว่าความยาวในแทร็กซ้ำจะไม่ทำให้มันพันรอบหากพฤติกรรมดังกล่าวเป็นที่ต้องการ ให้พิจารณาชิ้นส่วนโค้ดต่อไปนี้:
local newPosition = 1.5if newPosition >= sound.TimeLength thennewPosition = newPosition - sound.TimeLengthendsound.TimePosition = newPosition
ตัวอย่างโค้ด
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
ปริมาณของ Sound สามารถตั้งค่าระหว่าง 0 และ 10 และเป็นค่าเริ่มต้นสำหรับ 0.5
โปรดทราบว่าหาก Sound เป็นสมาชิกของ SoundGroup ระดับเสียงการเล่นของมัน (แต่ไม่ใช่คุณสมบัติ Volume ของมัน) จะได้รับอิทธิพลจากคุณสมบัติ SoundGroup.Volume ของกลุ่ม
วิธีการ
Pause
วิธีนี้หยุดการเล่นของ Sound หากกำลังเล่น ตั้งค่า Playing เป็น falseไม่เหมือนกับ Stop() มันไม่รีเซ็ต TimePosition ซึ่งหมายความว่าเสียงสามารถดำเนินการต่อได้โดยใช้ Resume()
ส่งค่ากลับ
ตัวอย่างโค้ด
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
วิธีนี้เล่น Sound และตั้ง TimePosition เป็นค่าล่าสุดที่กำหนดโดยสคริปต์ (หรือ 0 ตั้งค่า) จากนั้นตั้ง Playing เป็น true
ส่งค่ากลับ
ตัวอย่างโค้ด
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
วิธีนี้จะดำเนินการต่อ Sound และตั้ง Playing เป็น trueไม่เปลี่ยนแปลง TimePosition และดังนั้นจึงสามารถใช้เพื่อดำเนินการเล่นซ้ำเสียงที่หยุดชั่วคราวผ่าน Pause()
ส่งค่ากลับ
ตัวอย่างโค้ด
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
วิธีนี้หยุด Sound และตั้ง Playing เป็น false จากนั้นตั้ง TimePosition เป็น 0
ส่งค่ากลับ
ตัวอย่างโค้ด
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
อีเวนต์
DidLoop
เกิดไฟไหม้เมื่อใดก็ตามที่ลูป Sound จะเล่นส่งคืน soundId และ numOfTimesLooped โดยให้รหัสเนื้อหาของเสียงและจํานวนครั้งที่ซ้ำตามลําดับตามลําดับ
เมื่อ Sound ถูกหยุดผ่าน Stop() คอนเทนเนอร์ลูปจะรีเซ็ตความหมายของอีเวนต์ถัดไป DidLoop สำหรับ 1 สำหรับ numOfTimesLooped
พารามิเตอร์
ตัวอย่างโค้ด
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
จะเกิดไฟไหม้เมื่อ Sound ได้สิ้นสุดการเล่นและหยุด อีเวนต์นี้มักจะใช้ในการทําลายเสียงเมื่อเสร็จสิ้นการเล่น:
sound:Play()sound.Ended:Wait()sound:Destroy()
โปรดทราบว่าอีเวนต์นี้จะไม่ ไม่ได้ ยิงสําหรับเสียงที่ตั้งค่าเป็น ปิด ไม่ได้ ยิงเมื่อเสียงถูกหยุดก่อนที่การเล่นจะสำเร็จ; สำหรับการใช้งานนี้ให้ใช้เหตุการณ์ Stopped
พารามิเตอร์
Loaded
จะเกิดไฟไหม้เมื่อ Sound โหลดแล้ว
เนื่องจากอีเวนต์นี้จะเปิดไฟเฉพาะในเวลาที่โหลดเสียงเท่านั้นจึงแนะนำให้ตรวจสอบคุณสมบัติของเสียง IsLoaded ก่อนที่จะเชื่อมต่อกับอีเวนต์นี้
พารามิเตอร์
ตัวอย่างโค้ด
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
ไฟไหม้เมื่อใดก็ตามที่ Sound ถูกหยุดโดยใช้ Pause()
พารามิเตอร์
ตัวอย่างโค้ด
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
จะเกิดไฟไหม้เมื่อใดก็ตามที่ Sound ถูกเล่นโดยใช้ Play()อีเวนต์นี้จะ ไม่ได้ ยิงหาก Sound ถูกเล่นเนื่องจาก PlayOnRemove ถูกตั้งค่าเป็น true และเสียงถูกทําลาย
พารามิเตอร์
ตัวอย่างโค้ด
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
จะเกิดไฟไหม้เมื่อ Sound ถูกดำเนินการต่อโดยใช้ Resume()
พารามิเตอร์
ตัวอย่างโค้ด
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
จะเกิดไฟไหม้เมื่อ Sound ถูกหยุดโดยใช้ Stop() การทําลายเสียงในขณะที่เล่นจะไม่ทําให้เกิดเหตุการณ์นี้ไฟไหม้
พารามิเตอร์
ตัวอย่างโค้ด
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