Sound 是一個發出聲音的對象。當放置在 BasePart 或 Attachment 中時,此對象將從該部分的 BasePart.Position 或附件的 Attachment.WorldPosition 發出聲音。在這個放置中,Sound 展示了杜比效果,意味著它的頻率和傾斜會隨著附帶的任何附件或零件的相對運動而變化。此外,它的音量將由客戶端的聲音聆聽器(默認位置為 Camera 位置) 和聲音的父元素位置之間的距離決定。欲知更多資訊,請參閱 RollOffMode 。
如果音效是 不 被父輩到 BasePart 或 Attachment 的話,就會被認為是「全球」的。在這種情況下,聲音將在整個空間方播放相同的音量。
範例程式碼
這個樣本中的代碼展示了將音效傳給零件或附件的代碼如何在本地播放,玩家的相機越遠離零件,體驗音量下降的情況。
一個零件被實例化,並將聲音實例化並指派給零件。點擊偵測器設置在檢查聲音是否播放的零件上,使用 Sound.IsPlaying 來播放或停止聲音。
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
-- 創建聲音
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- 減少發射器尺寸(對於早期的量產放棄)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- 切換播放/未播放聲音
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 。
只讀屬性,在 Sound 播放時返回 true 。
一個範圍,用於標示在 PlaybackRegion 內的所需循環開始和循環結束,在秒內。
設置是否重複播放 Sound 一旦播放完畢。
當 true 時,Sound將在從體驗中移除時播放。
一個數字,介於 0 和 1000 之間,表示 Sound 目前播返回的音量。
一個範圍,表示在 TimeLength 內的期望起始和停止時間,以秒為單位。
如果 true , 此屬性會給你的 Sound 訪問 PlaybackRegion 和 LoopRegion 屬性, 這些屬性可以更準確地控制其播放。
決定 Sound 會以何種速度玩 遊玩的速度,高值會使聲音以更快的速度和更高的音高播放。
指示是否 Sound 正在播放。
客戶端的聆聽器最大距離,以噸為單位,可以從聲音的起源處到達,仍然能聽到它。只適用於 Sounds 被父輩拖到 BasePart 或 Attachment 。
距離最小,以厘米為單位,在那裡一個 Sound 被父輩到一個 BasePart 或 Attachment 開始減弱 (音量下降)。
控制如何減少Sound到BasePart或Attachment之間的距離變化時父級的音量(褪色)。
與此 Sound 相連的 SoundGroup 。
與 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() 應該使用。
範例程式碼
這個代碼示例包含當 Sound.IsPlaying 和 Sound.IsPaused 屬性為真或為假時的示範。
在工作區和 Sound.IsLoaded 屬性中啟用聲音,以確保已載入聲音,如果沒有聲音載入事件被使用來生成腳本直到聲音載入。
隨著聲音播放、暫停和停止,Sound.IsPlaying 和 Sound.IsPaused 屬性會被列印來展示它們如何回應這些功能。注意「暫停聲音」的聲音會永遠是真實的,即使聲音已停止而不是暫停。
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) -- 真、假
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- 假,真
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- 真、假
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true
IsPlaying
這個只讀屬性在 Sound 播放時返回真值。
因為 IsPlaying 是閱讀僅限,無法用於播放聲音;Play() 應該使用。
範例程式碼
這個代碼示例包含當 Sound.IsPlaying 和 Sound.IsPaused 屬性為真或為假時的示範。
在工作區和 Sound.IsLoaded 屬性中啟用聲音,以確保已載入聲音,如果沒有聲音載入事件被使用來生成腳本直到聲音載入。
隨著聲音播放、暫停和停止,Sound.IsPlaying 和 Sound.IsPaused 屬性會被列印來展示它們如何回應這些功能。注意「暫停聲音」的聲音會永遠是真實的,即使聲音已停止而不是暫停。
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) -- 真、假
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- 假,真
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- 真、假
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- false, true
LoopRegion
一個範圍,用於標示在 PlaybackRegion 內的所需循環開始和循環結束,在秒內。
如果 LoopRegion.Min > PlaybackRegion.Min , 循環從 LoopRegion.Min 開始。
如果 LoopRegion.Min < 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 。這意味著以下所有內容都會導致音響播放時 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 > Sound.TimeLength , 聲音會在 Sound.TimeLength 停止。
如果 PlaybackRegion.Max < Sound.TimeLength , 聲音會在 正確地 那時停止。
如果 PlaybackRegion.Min == PlaybackRegion.Max , 此屬性未啟用。
PlaybackSpeed
決定 Sound 會以何種速度玩 遊玩的速度,高值會使聲音以更快的速度和更高的音高播放。
範例程式碼
在這個例子中,在工作區播放了一個聲音。播放速度屬性在間隔內增加和減少以展示其對聲音播放的影響。
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- 3 倍更快
task.wait(5)
sound.PlaybackSpeed = 0.5 -- 2 倍慢
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 屬性。
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 是 SoundGroup 的成員,其播放音量 (但不是其 Volume 屬性) 將受到群組的 SoundGroup.Volume 屬性的影響。
方法
Pause
此方法會暫停播放 Sound 如果正在播放,將 Playing 設置為 false 。與 Stop() 不同,它不會重置 TimePosition , 即音響可以使用 Resume() 繼續播放。
返回
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Play
此方法播放 Sound 並將 TimePosition 設為由腳本設置的最後值 (或 0 如果尚未設設定),然後將 Playing 設為 true 。
返回
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Resume
這個方法會重新啟動 Sound 並將 Playing 設為 true 。不會更改 TimePosition ,因此可以用來恢復播放被暫停的聲音通過 Pause() 。
返回
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Stop
這個方法停止 Sound 並將 Playing 設為 false,然後將 TimePosition 設為 0。
返回
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 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()
請注意,此事件將 不 發射用於 Looped 設為 true 的聲音,因為它們在達到結束點時繼續播放。此事件也不會在播放完成前停止聲音時發生;為此使用事件。
參數
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
參數
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Played
在 Sound 使用 Play() 播放時,隨時發射。此事件將 不 發射,如果 由於 被設置為 而播放,音效被摧毀。
參數
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Resumed
在 Sound 使用 Resume() 重新啟動時發生火災。
參數
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0
Stopped
當 Sound 通過使用 Stop() 被停止時,發生火災。播放時摧毀聲音不會導致此事件發生。
參數
範例程式碼
這個範例提供了每個聲音功能(聲音播放、聲音停止、聲音暫停和聲音恢復)對聲音播放和聲音時間位置的簡單示範。
-- 創建聲音
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- 聆聽事件
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) -- 播放真實的 0
task.wait(10)
sound:Pause()
print("pause", sound.Playing, sound.TimePosition) -- 暫停 false 10
task.wait(3)
sound:Resume()
print("play", sound.Playing, sound.TimePosition) -- 播放真實 10
task.wait(3)
sound:Stop()
print("stop", sound.Playing, sound.TimePosition) -- 停止假 0
task.wait(3)
sound:Play()
print("play", sound.Playing, sound.TimePosition) -- play true 0