Sound 是发出声音的对象。当放置在 BasePart 或 Attachment 中时,该对象会从该部分的 BasePart.Position 或附件的 Attachment.WorldPosition 发出声音。在这个布置中,一个 Sound 展示了杜波尔效果,即其频率和倾斜与附加的任何附件或部件相对运动有关。此外,它的音量将由客户端的音频收听器(默认为 Camera 位置)和音效的父元素位置之间的距离决定。了解更多信息,请参阅RollOffMode。
声音被认为是“全球”的,如果它不是属于父辈于一个或或一个》。在这种情况下,音效将在整个场景方的同一音量播放。
代码示例
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)
概要
属性
当 Sound 从 Roblox 服务器加载并准备玩游戏时,此属性为 true 。
只读属性,当 Sound 不播放时返回 true 。
只读属性,在 true 播放时返回 Sound。
一个范围,用于标示在 PlaybackRegion 内的所需循环开始和循环结束,在秒内。
设置是否在播放完成后重复 Sound 。
当 true 时,Sound 将在被移除体验时播放。
一个数字,介于 0 和 1000 之间,表示 Sound 目前播返回的音量。
一个范围,用于标示 TimeLength 内的所需的起始和停止时间,以秒为单位。
如果 true , 这个属性可以让您的 Sound 访问 PlaybackRegion 和 LoopRegion 属性,这些属性可以更准确地控制其播放。
决定 Sound 游玩放的速度,高值会导致声音更快地播放并处于更高的音高。
指示 whether Sound 是否正在播放。
客户端的收听器的最大距离,以螺柱计算,可以从音效的起源处到达,仍然能听到它。仅适用于 Sounds 向 BasePart 或 Attachment 父辈。
在螺柱中最小的距离, 以学分为单位, 在这里一个 Sound 被父辈到一个 BasePart 或 Attachment 将开始减弱(音量下降).
控制父辈到一个 Sound 或 BasePart 或 Attachment 的音量如何减弱 (消失) 随着听众和父辈之间的距离变化。
与此 SoundGroup 相连的 Sound 。
与 Sound 相关的音频文件的内容ID。
在秒内的 Sound 长度。
Sound 的音量。
方法
活动
属性
ChannelCount
IsLoaded
当 Sound 从 Roblox 服务器加载并准备游玩游戏时,此属性为 true 。您可以使用此属性和 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 开始。
如果 LoopRegion.Max > PlaybackRegion.Max , 循环从 PlaybackRegion.Max 开始。
如果 LoopRegion.Max < PlaybackRegion.Max , 循环在 正确的时间开始 .
如果 LoopRegion.Min == 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 时,Sound 将在被父辈或其祖先移除体验时播放,如果它的祖先是 Sound 或其祖先是 nil 。这意味着以下所有内容都会导致声音在 播放时:
- 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 > Sound.TimeLength , 音效停止在 Sound.TimeLength 。
如果 PlaybackRegion.Max < Sound.TimeLength , 音效在 那一刻停止 .
如果 PlaybackRegion.Min == PlaybackRegion.Max , 此属性处于不活跃状态。
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
指示 whether 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。
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 在秒内的进度。它可以更改以移动音效的播放位置,在播放之前和播放期间。
当一个 Sound 播放时,TimePosition 以每秒钟 PlaybackSpeed 的速率增加。一旦 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 , 分别给出音频的内容 ID 和循环次数。
当 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() 播放时。此事件将在 不 发射,如果 由于被设置为 并音效被摧毁而播放。
参数
代码示例
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