Sound
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Sound là một đối tượng phát ra âm thanh.Khi được đặt trong một BasePart hoặc một Attachment , đối tượng này sẽ phát ra âm thanh từ phần BasePart.Position hoặc kèm theo của nó Attachment.WorldPosition .Trong vị trí này, một Sound triển lãm hiệu ứng Doppler, có nghĩa là tần số và độ cao của nó thay đổi theo tốc độ di chuyển tương đối của bất kỳ phụ kiện hoặc phần nào nó được gắn vào.Ngoài ra, âm lượng của nó sẽ được xác định bởi khoảng cách giữa người lắng nghe âm thanh của khách hàng (mặc định là vị trí Camera ) và vị trí của cha của âm thanh.Để biết thêm thông tin, xem RollOffMode .
Một âm thanh được coi là "toàn cầu" nếu nó không không được gán cho một BasePart hoặc một Attachment .Trong trường hợp này, âm thanh sẽ phát với cùng một âm lượng trên toàn bộ địa điểm.
Mẫu mã
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)
Tóm Tắt
Thuộc Tính
Thuộc tính này là true khi Sound đã được tải từ máy chủ Roblox và sẵn sàng chơi.
Tính năng chỉ đọc trả về true khi Sound không đang chơi.
Tính năng chỉ đọc trả về true khi Sound đang chơi.
Một phạm vi chỉ định một vòng lặp mong muốn bắt đầu và kết thúc trong PlaybackRegion , trong giây lát.
Bộ đặt xem có hay không có Sound lặp lại một lần khi nó đã kết thúc chơi.
Khi true , Sound sẽ chơi khi nó bị xóa khỏi trải nghiệm.
Một số giữa 0 và 1000 chỉ ra mức độ lớn mà Sound đang phát quay lạihiện tại.
Một phạm vi chỉ định thời gian bắt đầu và kết thúc mong muốn trong TimeLength, trong giây lát.
Nếu true , thuộc tính này cung cấp cho bạn Sound quyền truy cập vào PlaybackRegion và LoopRegion các thuộc tính có thể kiểm soát chính xác hơn việc phát lại của nó
Xác định tốc độ mà một Sound sẽ chơi, với các giá trị cao hơn gây ra âm thanh chơi nhanh hơn và ở độ cao hơn.
Chỉ ra liệu Sound có đang chơi hay không.
Khoảng cách tối đa, bằng studs, người lắng nghe của khách hàng có thể đến từ nguồn gốc của âm thanh và vẫn nghe thấy nó.Chỉ áp dụng cho Sounds bố mẹ đến một BasePart hoặc Attachment .
Khoảng cách tối thiểu, bằng đinh tán, tại đó một Sound mà được gán cho một BasePart hoặc Attachment sẽ bắt đầu giảm (giảm âm lượng).
Kiểm soát cách âm lượng của một Sound mà được gán cho một BasePart hoặc Attachment giảm (mờ dần) khi khoảng cách giữa người lắng nghe và cha mẹ thay đổi.
Các SoundGroup được liên kết với cái này Sound .
ID nội dung của tập tin âm thanh để liên kết với Sound .
Độ dài của Sound trong giây.
Tiến trình của Sound trong giây lát. Có thể thay đổi để di chuyển vị trí phát lại của Sound cả trước và trong lúc phát lại.
Âm lượng của Sound .
Phương Pháp
Thuộc Tính
ChannelCount
IsLoaded
Thuộc tính này là true khi Sound đã được tải từ máy chủ Roblox và sẵn sàng chơi.Bạn có thể sử dụng thuộc tính này và sự kiện Loaded để xác minh một âm thanh đã được tải trước khi chơi nó.
Mẫu mã
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
Tính tùy chỉnh này chỉ đọc chỉ trả về true khi Sound không đang chơi.Lưu ý rằng nó có thể trả về true nếu một âm thanh đã bị tạm dừng bằng cách sử dụng Pause() , nếu nó đã bị dừng bằng cách sử dụng Stop() , hoặc âm thanh chưa bao giờ được phát.
Vì là đọc chỉ đọc, nó không thể được sử dụng để ngăn chặn âm thanh; hoặc nên được sử dụng thay thế.
Mẫu mã
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
Thuộc tính chỉ đọc này trả về true khi Sound đang chơi.
Vì IsPlaying chỉ đọc, nó không thể được sử dụng để phát âm thanh; Play() nên sử dụng thay thế.
Mẫu mã
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
Một phạm vi chỉ định một vòng lặp mong muốn bắt đầu và kết thúc trong PlaybackRegion , trong giây lát.
Nếu Class.Sound.LoopRegion|LoopRegion.Min``>``Class.Sound.PlaybackRegion|PlaybackRegion.Min , vòng lặp bắt đầu từ LoopRegion.Min .
Nếu Class.Sound.LoopRegion|LoopRegion.Min``<``Class.Sound.PlaybackRegion|PlaybackRegion.Min , vòng lặp bắt đầu từ PlaybackRegion.Min .
Nếu Class.Sound.LoopRegion|LoopRegion.Max``>``Class.Sound.PlaybackRegion|PlaybackRegion.Max , vòng lặp bắt đầu tại PlaybackRegion.Max .
Nếu Class.Sound.LoopRegion|LoopRegion.Max``<``Class.Sound.PlaybackRegion|PlaybackRegion.Max , vòng lặp bắt đầu tại chính xác lúc đó.
Nếu Class.Sound.LoopRegion|LoopRegion.Min``==``Class.Sound.LoopRegion|LoopRegion.Max , thì Sound sử dụng thuộc tính PlaybackRegion thay thế.
Looped
Điều này xác định liệu có hay không có lặp lại Sound một khi nó đã kết thúc chơi.Các âm thanh lặp lại thích hợp cho một loạt ứng dụng bao gồm âm nhạc và âm thanh môi trường nền.
Sự kiện DidLoop có thể được sử dụng để theo dõi số lần lặp lại của âm thanh.
Mẫu mã
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
Khi true , Sound sẽ chơi khi nó bị xóa khỏi trải nghiệm bằng cách nuôi dưỡng Sound hoặc một nếu tổ tiên của nó là nil .Điều này có nghĩa là tất cả các điều sau sẽ gây ra âm thanh phát khi PlayOnRemove được true :
- sound:Destroy()
- sound.Parent = nil
- sound.Parent.Parent = nil
PlaybackLoudness
Một số giữa 0 và 1000 chỉ ra mức độ lớn mà Sound đang phát quay lạihiện tại.Tính chất này phản ánh độ lớn của việc phát lại âm thanh trong thời gian nó được đã xem.
Mẫu mã
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
Một phạm vi chỉ định thời gian bắt đầu và kết thúc mong muốn trong TimeLength, trong giây lát.
Nếu PlaybackRegion.Min``>``0 , âm thanh bắt đầu phát từ thời gian PlaybackRegion.Min .
Nếu PlaybackRegion.Min``<``0 , âm thanh bắt đầu phát từ 0 .
Nếu PlaybackRegion.Max``>``Class.Sound.TimeLength , âm thanh dừng lại tại Sound.TimeLength .
Nếu PlaybackRegion.Max``<``Class.Sound.TimeLength , âm thanh dừng lại tại chính xác lúc đó.
Nếu Class.Sound.PlaybackRegion|PlaybackRegion.Min``==``Class.Sound.PlaybackRegion|PlaybackRegion.Max , thuộc tính này không hoạt động.
PlaybackRegionsEnabled
Nếu true , thuộc tính này cung cấp cho bạn Sound quyền truy cập vào PlaybackRegion và LoopRegion các thuộc tính có thể kiểm soát chính xác hơn việc phát lại của nó
PlaybackSpeed
Xác định tốc độ mà một Sound sẽ chơi, với các giá trị cao hơn gây ra âm thanh chơi nhanh hơn và ở độ cao hơn.
Mẫu mã
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
Chỉ ra liệu Sound có đang chơi hay không. Bạn có thể bật/tắt chức năng này và thuộc tính này sẽ luôn sao chép.
Trong cửa sổ Tính năng của Studio, trong khi ở chế độ Chỉnh sửa , chuyển Playing``true không bắt đầu phát âm thanh, nhưng âm thanh sẽ bắt đầu phát trong quá trình chạy.
Tính chất này không nên nhầm lẫn với IsPlaying mà là một tính chất chỉ đọc.
Lưu ý rằng khi Playing được đặt thành false , thuộc tính TimePosition của âm thanh sẽ không được đặt lại, có nghĩa là khi Playing được đặt thành true một lần nữa, âm thanh sẽ tiếp tục từ vị trí thời gian nó đã dừng lại khi nó bị dừng.Tuy nhiên, nếu chức năng Play() được sử dụng để tiếp tục phát âm thanh, vị trí thời gian sẽ được đặt lại thành 0 .
Mẫu mã
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
Khoảng cách tối đa, bằng studs, người lắng nghe của khách hàng có thể đến từ nguồn gốc của âm thanh và vẫn nghe thấy nó.Chỉ áp dụng cho Sounds bố mẹ đến một BasePart hoặc Attachment .
Cách RollOffMaxDistance ảnh hưởng đến sự giảm thiểu của một âm thanh (cách nó biến mất) phụ thuộc vào thuộc tính RollOffMode.
RollOffMinDistance
Khoảng cách tối thiểu, bằng đinh tán, tại đó một Sound mà được gán cho một BasePart hoặc Attachment sẽ bắt đầu giảm (giảm âm lượng).
Cách RollOffMinDistance ảnh hưởng đến sự giảm thiểu của một âm thanh (cách nó biến mất) phụ thuộc vào thuộc tính RollOffMode.
RollOffMode
Tính chất này kiểm soát cách âm lượng của một Sound được gán cho một BasePart hoặc Attachment giảm (mờ dần) khi khoảng cách giữa người lắng nghe và cha mẹ thay đổi.
Đối với chi tiết về các chế độ khác nhau, xem Enum.RollOffMode .
SoundId
Thuộc tính này là ID nội dung của tập tin âm thanh để liên kết với Sound . Xem Tài sản âm thanh để biết thêm thông tin.
TimeLength
Độ dài của Sound trong giây. Nếu Sound không được tải, giá trị này sẽ là 0 .
Tính năng này thường được sử dụng kết hợp với PlaybackSpeed để điều chỉnh tốc độ của một âm thanh để nó kéo dài trong một thời gian cụ thể.
Mẫu mã
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
Thuộc tính này phản ánh tiến trình của Sound trong vài giây.Nó có thể được thay đổi để di chuyển vị trí phát lại của âm thanh trước và trong khi phát lại.
Khi được chơi, tăng với tốc độ mỗi giây.Một khi TimePosition đạt đến TimeLength, âm thanh sẽ dừng lại trừ khi nó là Looped.
Lưu ý rằng việc đặt TimePosition đến một giá trị lớn hơn chiều dài trong một vòng lặp sẽ không làm cho nó cuộn quanh.Nếu hành vi đó được mong muốn, hãy xem xét các đoạn mã sau:
local newPosition = 1.5if newPosition >= sound.TimeLength thennewPosition = newPosition - sound.TimeLengthendsound.TimePosition = newPosition
Mẫu mã
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
Âm lượng của Sound . Có thể được đặt giữa 0 và 10 và mặc định là 0.5 .
Lưu ý rằng nếu Sound là thành viên của một SoundGroup , âm lượng phát lại của nó (nhưng không phải tính năng Volume của nó) sẽ bị ảnh hưởng bởi tính năng SoundGroup.Volume của nhóm.
Phương Pháp
Pause
Phương pháp này tạm dừng phát lại của Sound nếu nó đang phát, thiết lập Playing thành false .Không giống như Stop() , nó không đặt lại TimePosition , có nghĩa là âm thanh có thể được tiếp tục sử dụng bằng cách sử dụng Resume() .
Lợi Nhuận
Mẫu mã
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
Phương pháp này chơi Sound và đặt TimePosition vào giá trị cuối cùng được đặt bởi một kịch bản (hoặc 0 nếu nó chưa được cài đặt), sau đó đặt Playing vào true .
Lợi Nhuận
Mẫu mã
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
Phương pháp này tiếp tục Sound và đặt Playing vào true .Không thay đổi TimePosition và do đó có thể được sử dụng để tiếp tục phát lại một âm thanh bị tạm dừng thông qua Pause() .
Lợi Nhuận
Mẫu mã
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
Phương pháp này ngăn chặn Sound và đặt Playing đến false , sau đó đặt TimePosition đến 0 .
Lợi Nhuận
Mẫu mã
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
Sự Kiện
DidLoop
Bắt lửa bất cứ khi nào vòng lặp Sound lặp.Trả về soundId và numOfTimesLooped, cung cấp ID nội dung của âm thanh và số lần lặp lại tương ứng.
Khi Sound bị dừng lại thông qua Stop() , vòng lặp lại đếm lại có nghĩa là sự kiện tiếp theo DidLoop sẽ trả về 1 cho numOfTimesLooped .
Tham Số
Mẫu mã
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
Bắt lửa khi Sound đã hoàn thành phát lại và dừng lại. Sự kiện này thường được sử dụng để phá hủy một âm thanh khi nó đã hoàn thành phát lại:
sound:Play()sound.Ended:Wait()sound:Destroy()
Lưu ý rằng sự kiện này sẽ không bắt lửa cho các âm thanh với Looped được đặt thành true , khi chúng tiếp tục chơi khi đạt đến điểm kết thúccùng.Sự kiện này cũng sẽ không bắt lửa khi âm thanh bị dừng trước khi phát lại hoàn thành; đối với trường hợp này sử dụng sự kiện Stopped.
Tham Số
Loaded
Bắt lửa khi Sound được tải.
Vì sự kiện này chỉ bắt lửa khi âm thanh được tải, nên khuyến nghị kiểm tra thuộc tính IsLoaded của âm thanh trước khi kết nối với sự kiện này.
Tham Số
Mẫu mã
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
Bắt lửa mỗi khi Sound bị tạm dừng bằng cách sử dụng Pause() .
Tham Số
Mẫu mã
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
Bắt lửa mỗi khi Sound được chơi bằng cách sử dụng Play() .Sự kiện này sẽ không bắt lửa nếu Sound được chơi do PlayOnRemove được đặt thành true và âm thanh bị phá hủy.
Tham Số
Mẫu mã
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
Bắt lửa khi Sound được khởi động lại bằng cách sử dụng Resume() .
Tham Số
Mẫu mã
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
Bắt lửa khi Sound bị dừng bằng cách sử dụng Stop() . Phá hủy một âm thanh trong khi nó đang phát sẽ không gây ra sự kiện bắt lửa này.
Tham Số
Mẫu mã
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