Sound

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

一个 Sound 是一个发出声音的对象。

2D 和 3D 声音

放置声音在 BasePartAttachment 中,声音将从其父元素件的 Class.BasePart.

如果音效不是父级 至 BasePartAttachment ,声音将在整个场景方的所有地方播放。

代码示例

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

概要

属性

方法

活动

属性

ChannelCount

只读
未复制
不可浏览
Roblox 脚本安全性
读取并联

IsLoaded

只读
未复制
读取并联

Sound 从 Roblox 服务器加载时,此属性将是真实的,因为它已准备好播游玩。

在 Roblox 中,音频文件不存储在游戏本身,而是存储在 Roblox 服务器上,并由 Sound.SoundId 属性引用。这意味着它们需要先下载到客户端的设备才能播放。这可能需要一段时间,因为用户的互联网连接取决于用户的网络连接,音效长度和需要加载的其他对象。

开发人员可以使用 Sound.IsLoaded 属性和 Sound.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.

Load Sound

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

IsPaused

隐藏
只读
未复制
读取并联

此只读取属性将在 Sound 不播放时返回真。注意,此属性不仅在使用 Sound:Pause() 函数暂停声音时返回真,而且还会在使用 Sound:Stop() 函数或从未播放时停止使用。

此属性仅在 Sound.IsPlaying 为 false 时使用。

As IsPaused只能读取,它不能用来停止声音,Sound:Stop()Sound: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.

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

隐藏
只读
未复制
读取并联

Sound 正在播放时,此只读取属性将返回 true。

此属性只能在 Sound.IsPaused 为 false 时使用。

由于 As IsPlaying 只能读取,因此它不能用于播放声音, Sound: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.

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

读取并联

一个显示声音的 Sound.PlaybackRegion 在秒内显示声音的开始和结束。

Looped

读取并联

这将设置是否要在 Sound 完成后重复。

循环声音适合许多应用,例如音乐和背景环境声。 Sound.DidLoop 事件可以用于跟踪声音是否循环。

代码示例

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

读取并联

当真的时候,Sound 将在它从游戏中移除后播放。

注意,当 Instance.Parent 属性的 Sound 或其父亲设置为零时,声音将播放。这意味着当 PlayOnRemove 为真时,所有以下都会导致声音播放。注意,这包括 Instance:Destroy() 作为父亲设置为零的摧毁函数。

声音:Destroy() 声音。父亲 = nil 声音。父亲 = nil

代码示例

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

只读
未复制
读取并联

一个数字,数值在 0 和 1000 之间,表示 Sound 当前播返回的音量。

这个属性反映时间在已读取时声音的幅度。因此,对于大多数声音,它将不断波动。因此,它可以在 Roblox Studio 属性窗口中作为 0 显示,但当代码在命令栏或 Scripts 读取时,它将返回正确的值。

代码示例

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

读取并联

一个范围,表示在 Sound.TimeLength 内,最小开始时间(min)和最大停止时间(max)。

  • 如果 PlaybackRegion.Min > 0,声音从 PlaybackRegion.Min 时间开始播放。

  • 如果 PlaybackRegion.Min < 0,声音将从 0 开始播放。

  • 如果 PlaybackRegion.Max > Sound.TimeLength,声音会在 Sound.TimeLength 结束。

  • 如果 PlaybackRegion.Max < Sound.TimeLength,声音会在 正确 的时间停止。

  • 如果 PlaybackRegion.Min == PlaybackRegion.Max,则 PlayBackRegion 不活跃。

PlaybackRegionsEnabled

读取并联

如果 true ,此属性将您的 Sound 访问到 Sound.PlaybackRegion 和 2> Class.Sound.LoopRegion2> 属性,可以更准确地控制其播放。

PlaybackSpeed

未复制
读取并联

确定Sound的游玩放速度。值越大,声音就越快。

例如,一个值为 2 会导致 Sound 的播放速度为 2x 快,而一个值为 0.5 会导致它播放速度为 2x 慢。当 PlaybackSpeed 为 1 时,声音将需要 Sound.TimeLength (以秒为单位) 才能完成。

注意,提高音效的 PlaybackSpeed 会导致它以更高的音调播放。

代码示例

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

未复制
读取并联

指示 Sound 当前正在播放。这可以切换,这个属性将总是重复。

在工作室编辑器中,Sounds 不能播放,因为时间已停止。在编辑模式下将 Sound.Playing 设置为“真”,但在插件中设置 Sounds 作为 1> Class.PluginGui1> 的后代,都无法播放音频。 但您可以通

这个属性不应与 Sound.IsPlaying ,这是一个只读属性。 播放可以设置为 true 或 false 来开始或停止音效的播放。

注意当 Play 设置为 false 时,音效的 Sound.TimePosition 属性将不会重置。这意味着当 Play 设置为 true 时,音效将从停止时的时间位置恢复。 但如果使用 Sound: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.

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

读取并联

客户端的听器可以从Sound原始位置开始,并且仍然听到它。仅适用于与PartAttachment(3D声音)关联的声音。

如何 RollOffMaxDistance 影响音效的消失 (人们在消失后消失) 是由 Sound.RollOffMode 属性决定的。当 RollOffMode 设置为使用倒向类型距离模型 (Inverse 或 InverseTapered) 时,RollOffMaxDistance 不会影响音效的消失。

当 RollOffMode 设置为直线类型距离模型 (Linear 或 LinearSquared) 时,声音在 Sound.EmitterSize 和 MaxDistance 之间会有所减弱(播放音效在 RollOffMaxDistance 为零)。 这在某些情况下可能会允许手动处理。

RollOffMinDistance

读取并联

在螺距( studs )最小距离上,3D Sound (直接子女 of a BasePartAttachment ) 将开始减弱(在音量上减少)。

声音与 BasePartAttachment (默认为 Workspace 和 1> Class.Attach1> 的子父元素),在播放时受到

Class.Sound 的消失方式(褪色)在距离听众和音效之间超过 RollOffMinDistance 后由 RollOffMode 决定。

RollOffMode

读取并联

这个属性设置 3D Sounds 的消失(褪色)为与声音的父级距离增加。它可以设置为 Enum.RollOffMode 枚表。

以下代码将将 RollOffMode 设置为线性:


sound.RollOffMode = Enum.RollOffMode.Linear

不同的模式

可用的选项是:


<tbody>
<tr>
<td>倒转</td>
<td>音量从 <code>声音/滚动最小距离</code> 的角度减少。</td>
</tr>
<tr>
<td>反向Tapered</td>
<td>一个混合模型。 跟随 Inverse 模型,当靠近 <code>RollOffMinDistance</code> 和线性方块模型,当靠近 <code>Sound/RollOffMaxDistance</code> 。</td>
</tr>
<tr>
<td>直线</td>
<td>音量在 <code>RollOffMinDistance</code> 和 <code>Sound/RollOffMaxDistance</code> 之间的关系是线性的。</td>
</tr>
<tr>
<td>方形</td>
<td>音量在 <code>RollOffMinDistance</code> 和 <code>Sound/RollOffMaxDistance</code> 之间的渐变。</td>
</tr>
</tbody>
模式描述

逆向 vs 线性距离减震

默认音效设置为使用逆向距离调整(Enum.RollOffMode.Inverse),这会在实时世界中声音的减衰方式。在逆向距离调整下,声音将在距离听众和音效父亲之间的距离超过滚动距评分时开始减衰。距离的大小取决于发射器的大

在倒退模型下,RollOffMaxDistance 不会影响音效的减衰,但会使音效完全切断,当距离达到此时。 这可能会在使用低值为最大距离时特别突然。

按线距离补偿工作不同。 在按线距离补偿中,声音会在 RollOffMinDistance 和 RollOffMaxDistance 之间减弱,直到 MaxDistance 达到。 在 MaxDistance 达到时,声音的渐变将被消除。 但是,在反向距离补偿模型中,在 EmitterSize 和 MaxDistance

SoundGroup

读取并联

SoundGroup 是链接到此 Sound 的。SoundGroup.Volume 和 2>Class.SoundEffect|SoundEffects2> 对于此音组都适用。一个音只能在一个音组上同时存在。

SoundGroups 用于管理多个Sounds 的音量和效果。一个Sound 添加到一个0> Class.SoundGroup0> 通过设置音音的声音组属性。

代码示例

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
读取并联

此属性是与Sound对象关联的音频文件的内容ID。一旦音频已上传到Roblox,内容ID可以在上传的音频中找到。

重要的是,请记住 URL 不会与内容 ID 相同。它将在 Roblox Studio 中直接粘贴到 Sound 的内容ID,因为 Studio 会自动将其修复,但如果它是从 Script 设置的话,就需要使用从 URL 中获取的数字。例如:


"https://www.roblox.com/catalog/9120386436" -- 网页 URL (不会工作)
"http://www.roblox.com/asset/?id=9120386436" -- 内容ID (将会有效)
"rbxassetid://9120386436" -- Content ID (alternative version, will work)

代码示例

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

只读
未复制
读取并联

Class.Sound 的长度以秒为单位。如果 Sound 未加载,此值将为 0。

此属性常常与 Sound.PlaybackSpeed 结合,用于调整音效的持续时间(见例子)。当 Sound.PlaybackSpeed 等于 1 时,声音将需要 1 秒的时间长度才能完成。

代码示例

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

未复制
读取并联

显示Sound的进度。可以更改进度来移动音效的播放位置。如果音效正在播放,则会将播放位置卡在指定位置。如果未播放Sound,将在声音下次播放时开始播放。

Sound 播放时,时间位置每秒增加 Sound.PlaybackSpeed 。一旦时间位置达到 Sound.TimeLength ,声音将停止,除非设置 1> Class.Sound.Looped1> 为 true

注意将TimePosition设置为超过循环轨道长度的值不会导致包围。如果该行为是想要的,开发者应该执行以关注中/正在关注操作。

如果新Position >= sound.TimeLength ,新Position = newPosition - sound.TimeLength 结束声音。Position = newPosition

将 TimePosition 设置为零以下不会影响播放,但此行为不应被视为可靠。

代码示例

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

读取并联

Class.Sound 的音量。可以设置在 0 和 10 之间。默认为 0.5

注意,如果 SoundSoundGroup 的成员,其播放音量(但不是音量属性)将受到 SoundGroup.Volume 的音量属性影响。该效果是乘数的,因此如果音量为 0.1 ,其

代码示例

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

方法

Pause

void

Sound.Playing 设置为 false。这会暂停音效的播放,如果音效正在播放。与 Sound 不同,它不会重置 Sound:Stop() ,意味着声音可以使用 1> Class.Sound:Resume()1> 继续。

以下表中显示了不同的音效对 Sound.PlayingSound.TimePosition 的影响。


<tbody>
<tr>
<td>声音:播放()</td>
<td>真的</td>
<td>在 Lua 中设置的最后一个值 (默认 0)</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>错误</td>
<td>-</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>真的</td>
<td>-</td>
</tr>
<tr>
<td>声音:停止()</td>
<td>错误</td>
<td>0</td>
</tr>
</tbody>
函数声音。播放声音。时间位置

返回

void

代码示例

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

播放 Sound 。将 Sound.TimePosition 设置为最后一个由 Script 设置的值 (或 0 如果未设置),然后将 2>Class.Sound.Playing2> 设置为 true。

下表中,示示了不同 Sound 函数对 Sound.PlayingSound.TimePosition 的影响。


<tbody>
<tr>
<td>声音:播放()</td>
<td>真的</td>
<td>在 Lua 中设置的最后一个值 (默认 0)</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>错误</td>
<td>-</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>真的</td>
<td>-</td>
</tr>
<tr>
<td>声音:停止()</td>
<td>错误</td>
<td>0</td>
</tr>
</tbody>
函数声音。播放声音。时间位置

返回

void

代码示例

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

重新创建 Sound 。设置 Sound.Playing 为 true。不会改变 Sound.TimePosition ,因此可以用于重新启动使用 2>Class.Sound:Pause()2> 的声音。

下表中,示示了不同音效对 Sound.PlayingSound.TimePosition 的影响。


<tbody>
<tr>
<td>声音:播放()</td>
<td>真的</td>
<td>在 Lua 中设置的最后一个值 (默认 0)</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>错误</td>
<td>-</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>真的</td>
<td>-</td>
</tr>
<tr>
<td>声音:停止()</td>
<td>错误</td>
<td>0</td>
</tr>
</tbody>
函数声音。播放声音。时间位置

返回

void

代码示例

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

停止 Sound 。将 Sound.Playing 设置为 false 然后将 Sound.TimePosition 设置为 0。

下表中,示示了不同音效对 Sound.PlayingSound.TimePosition 的影响。


<tbody>
<tr>
<td>声音:播放()</td>
<td>真的</td>
<td>在 Lua 中设置的最后一个值 (默认 0)</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>错误</td>
<td>-</td>
</tr>
<tr>
<td>声音:暂停()</td>
<td>真的</td>
<td>-</td>
</tr>
<tr>
<td>声音:停止()</td>
<td>错误</td>
<td>0</td>
</tr>
</tbody>
函数声音。播放声音。时间位置

返回

void

代码示例

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

活动

DidLoop

发生在 Sound 循环时触发的事件。返回声音ID和numOfTimesLooped,分别为音效和循环次数提供内容。

Sound 被停止时,循环反馈意味着下一个 DidLoop 事件将返回 1 对数。

参数

soundId: string

Class.Sound.SoundId 的 Sound ,循环。

numOfTimesLooped: number

Class.Sound 的循环次数。


代码示例

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

Sound 完成播放并停止时触发。注意此事件不会在声音设置为 Sound.Looped 设置为 true 时发生,因为它们在结束时继续播放。

此事件通常用于当播放完成后摧毁声音。


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

此事件仅发生 if 音效 已到达其结束。 这意味着在播放完成前声音也不会发生,因此使用 Sound.Stopped

参数

soundId: string

Class.Sound.SoundId 的 Sound 已结束。


Loaded

声音加载事件触发,当 Sound 加载。

注意,此事件仅发生声音加载时。 这意味着如果声音已加载,它将不会返回传。 因此,建议在连接到此事件之前检查 Sound.IsLoaded

参数

soundId: string

Class.Sound.SoundId 的 Sound 加载。


代码示例

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

使用 Sound 暂停 Sound:Pause() 时触发。

Sound.PlayedSound.ResumedSound.Stopped 仅限音效会导致事件触发。这意味着暂停只会触发当 1> Class.Sound:Pause()1> 被调用时。

参数

soundId: string

Class.Sound.SoundId 的 Sound 暂停。


代码示例

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

使用 Sound 函数播放 Sound:Play() 时,火焰会随时发生。

Sound.StoppedSound.PausedSound.Resumed 仅限声音函数才会触发事件。这意味着 Played 只会触发当 1> Class.Sound:Play

参数

soundId: string

播放的 Class.Sound.SoundId 的 Class.Sound。


代码示例

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

使用 Sound 重新启动 Sound:Resume() 时触发。

Sound.PlayedSound.PausedSound.Stopped 仅限音效会导致事件触发。这意味着 Resume 只会触发在 1> Class.Sound:Resume()1> 被调用时。

参数

soundId: string

Class.Sound.SoundId 的 Sound 正在恢复。


代码示例

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

Sound 函数由于 Sound:Stop() 函数而停止时触发。

Sound.PlayedSound.PausedSound.Resumed 仅限于声音功能,会导致事件触发。这意味着停止只会触发当 1>Class.Sound:Stop1> 被调用时。当声音正在播放时,会触发此事件。

参数

soundId: string

Class.Sound.SoundId 的 Sound 已停止。


代码示例

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