Sound

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

Class.Sound 是一個發出聲音的對象。

2D 和 3D 音效

放置聲音在 BasePartAttachment 中的聲音將從該部

如果音效不屬於 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 播放時返回真。

此屬性只能在 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 被從遊戲中移除時,Class.Sound 將會播放。

注意音效將在 Instance.Parent 或其祖先設置為零時播放。這表示所有以下將會在 Sound 啟用時播放。 注意,這包括 Instance:Destroy() 作為摧毀功能將父親設置為零。

音效:Destroy() 音效。Parent = nil 音效。Parent = 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

唯讀
未複製
平行讀取

一個數字,指示 Sound 正在播返回的音量。

這個屬性代表音效在時間過程中播放的幅度。因此,對於大多數音效來說,它們會在 Roblox Studio 屬性窗口中反復變化。因此,當代碼在命令條或 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

平行讀取

一個範圍,表示要開始 (min) 和停止 (max) 時間在 Sound.TimeLength 內,以秒計。

  • 如果 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 和 1> Class.Sound.LoopRegion1> 屬性,這可以更準確地控制其播放。

PlaybackSpeed

未複製
平行讀取

決定 Sound 會玩 遊玩放的速度。值越大,音效就越快。

舉例來說,Sound的值為 2 會使 Sound.TimeLength (在秒鐘) 的音效播放速度加快 2 倍,而Class.Sound.TimeLength (在秒鐘) 的音效播放速度會加快 2 倍。當0>PlaybackSpeed0>等同於 1 時,音效將需要 3>Class.Sound.TimeLength3> (在秒鐘) 才能

注意,提升音效的 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 是否正在播放。這項功能可以切換,而此屬性將永遠重複。

在 Studio 編輯器中,Sounds 不能播放,因為時間已停止。在編輯模式中將 Sound.Playing 設置為Sounds 的後代,卻不會發生任何效果。但在編輯模式中,您可以從1> Class.Sound|Sounds</

這個屬性不應與 Sound.IsPlaying ,這是一個只讀取的屬性。 Playing 可以設為 true 或 false 來啟動或停止音效的播放。

注意,當 Play 設為 false 時,音樂的音量將不會重置。這表示當 Play 設為 true 時,音樂會從停止時的位置恢復。但如果使用 <a href="https://play.az-m.com/play/play.az-m.com/play/play.az-m.com/play/play.az-m.com/play/play.az-m.com/play/play.az-m.com/play/play.az-m.com/play/play.

範例程式碼

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聲音)。

滾動最大距離 (滾動最大距離模型的樣式) 會影響音效的消失方屬性。當滾動模式設為使用倒帶或倒帶減速模型 (倒帶或倒帶減速模型) 時,滾動最大距離不

將 RollOffMode 設為線性類型距離模型 (Linear 或 LinearSquared) 時,音效會在 Sound.EmitterSize 和 MaxDistance 之間減弱。這是一種較不實,但在一些情況下可以更直觀地處理壓縮。

RollOffMinDistance

平行讀取

在 stud 單位的最小距離,在 3D Sound (直接子供 of a BasePartAttachment ) 將開始減少 (在音量上降低)。

聲音與 BasePartAttachment 下一個是 Workspace 或 1>Class.附件1> 的父元素 ,這是 3D 音效的音量取決於

Class.Sound 的消失方式 (漸漸消失) 是根據 Class.Sound 的距離 (距離音效和音效之間的距離) 決定的。

RollOffMode

平行讀取

此屬性設定 3D Sounds 的減漸方式 (漸出) 作為聆聽器和音效的距離增加。它可以設為 Enum.RollOffMode 枚的值。

以下代碼將將 RollOffMode 設置為線性:


sound.RollOffMode = Enum.RollOffMode.Linear

不同的模式

可用的選項是:


<tbody>
<tr>
<td>反向</td>
<td>音量從 <code>Sound/RollOffMinDistance</code> 減少。</td>
</tr>
<tr>
<td>反向輕觸</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>
模式說明

反向與直線距離減少

預設音效設定為使用反向距離減少 (Enum.RollOffMode.Inverse),這會在音效在實際世界中如何減少的方式。在反向距離減少中,音效將會開始減少一旦距離之間的距評分超過 RollOffMinDistance 。音效的減少

滾動最大距離不會在倒轉模型下影響衰減,但會導致音效完全切斷,一旦這個距離達到。這可能會導致低值的最大距離無法使用。

線性距離衰減會以不同的方式運作。 在線性距離衰減中,聲音會在 RollOffMinDistance 和 RollOffMaxDistance 之間減輕,直到 MaxDistance 達到。 在 RollOffMinDistance 達到時,聲音會開始衰減。 但是,在反

SoundGroup

平行讀取

連接到這個 SoundGroupSoundSoundGroup.Volume 和 2>Class.SoundEffect|SoundEffects2> 將適用於此音效群。一個音效群只能在一個音效群上存在。

SoundGroups 用於管理多個 Sounds 的音量和效果。 A 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
平行讀取

此屬性是音效檔案的內容 ID,與 Sound 對象對應。一旦音效已上傳至 Roblox 內容 ID 可以在上傳的音效中找到。

重要的是,記住 URL 與內容 ID 不相同。它會在 Roblox Studio 中直接貼入到 Sound 的內容,因為 Studio 會自動修復它,但如果從 Script 設置,則需要使用從 URL 中取得的數字。 為例:


"https://www.roblox.com/catalog/9120386436" -- 網頁網址 (無法作業)
"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 相等時,音效將需要的時間來完成。

範例程式碼

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 時,音效將停止,除非它是鏈接。這意味著,除非

注意設置 TimePosition 為超過長度在重複軌道上的值,不會導致它包起來。如果要求此行為,開發人員應該執行以追蹤中操作。

如果新Position >= sound.TimeLength ,則新Position = 新Position - sound.TimeLength 結束音效。

將 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 的音量 1>Class.SoundGroup.Volume1> 影響。效果是乘數,

範例程式碼

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.TimePosition1> ,意味著音效可以使用 4>Class.Sound:Res

不同的音效對 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> 為真。

下表顯示了 SoundSound.Playing 對影響 Sound.TimePosition 和 1>Class.Sound.TimePosition1> 的影響。


<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 為真。不會變更 Sound.TimePosition ,因此可以用於重新播放已停止使用 1>Class.Sound:Pause1> 的聲音。

不同音效對 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,分別為音效和數次循環的內容提供ID。

Sound 被停止時,由 looped 的反向計數器重置,意味著下一個 DidLoop 事件將返回 1 以 numOfTimesLooped 的意思。

參數

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:Play()
sound.Ended:Wait()
sound:Destroy()

這個事件只會在音效到達其結束點時才會發生。這意味著在播放完成前,音效也不會發生,因此使用 Sound.Stopped

參數

soundId: string

結束的 Class.Sound.SoundId 的 Class.Sound。


Loaded

Sound.Loaded 事件會在 Sound 載入時發生。

注意這個事件只會在音效載入時發生。這意味著如果聽到音效載入時已載入,它就不會傳回回。因此,建議在連接到此事件之前檢查 Sound.IsLoaded

參數

soundId: string

載入的 Class.Sound.SoundId 的 Class.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

發射 Class.Sound:Pause() Class.Sound:Pause 使用 Class.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

發射 Class.Sound:Play() Class.Sound:Play 函數使用。

Sound.StoppedSound.PausedSound.Resumed 只有音效相關的音效功能才會造成事件發生。這意味著 Played 只會發生在

參數

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 一樣,只有 1>Class.Sound.Stopped1> 的音效才會造成事件發觸發。這意味著 4>Resume4> 只會在 7>Class.Sound:Resume()7> 被呼叫時才會發生。

參數

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

發生在 SoundSound:Stop() 函數停止時。

Sound.PlayedSound.PausedSound.Resumed 一樣,只有 1>Class.Sound:Stop1> 會造成事件發觸發。這意味著 4>Stop4> 只會在 7>Class.Sound:Stop7> 被呼叫時才會發觸發。摧毀一

參數

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