Lớp công cụ
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.
Tóm Tắt
Thuộc Tính
SoundId:ContentId |
Sự Kiện
DidLoop(soundId: string,numOfTimesLooped: number):RBXScriptSignal |
Ended(soundId: string):RBXScriptSignal |
Loaded(soundId: string):RBXScriptSignal |
Paused(soundId: string):RBXScriptSignal |
Played(soundId: string):RBXScriptSignal |
Resumed(soundId: string):RBXScriptSignal |
Stopped(soundId: string):RBXScriptSignal |
Mẫu mã
Phần Chơi Nhạc
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
-- tạo một âm thanh
local sound = Instance.new("Sound", part)
sound.SoundId = "rbxassetid://9120386436"
sound.EmitterSize = 5 -- giảm kích thước bộ phát (để giảm âm lượng sớm hơn)
sound.Looped = true
sound.Parent = part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
-- chuyển đổi giữa âm thanh đang phát / không phát
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ài Liệu Tham Khảo API
Thuộc Tính
EmitterSize
IsLoaded
Mẫu mã
Tải Âm Thanh
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Âm thanh đã được tải!")IsPaused
Mẫu mã
Âm thanh Đang chơi và Âm thanh Được tạm dừng
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) -- đúng, sai
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- đúng, sai
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúngIsPlaying
Mẫu mã
Âm thanh Đang chơi và Âm thanh Được tạm dừng
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) -- đúng, sai
task.wait(2)
sound:Pause()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúng
task.wait(2)
sound:Play()
print(sound.IsPlaying, sound.IsPaused) -- đúng, sai
task.wait(2)
sound:Stop()
print(sound.IsPlaying, sound.IsPaused) -- sai, đúngisPlaying
Looped
Mẫu mã
Lặp một số lần
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
-- ngắt kết nối
connection:Disconnect()
-- dừng âm thanh
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)MaxDistance
MinDistance
Pitch
PlaybackLoudness
Mẫu mã
Thanh Biên Độ Âm Lượng
-- đặt trong StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- đợi người chơi cục bộ PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- tạo một ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- tạo một khung cho thanh của chúng ta
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
-- tạo một thanh
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
-- tạo một âm thanh
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- định nghĩa độ lớn tối đa
local maxLoudness = 30
-- hoạt hình thanh biên độ
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackSpeed
Mẫu mã
Tốc độ phát lại âm thanh
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
sound:Play()
task.wait(10)
sound.PlaybackSpeed = 3 -- nhanh gấp 3 lần
task.wait(5)
sound.PlaybackSpeed = 0.5 -- chậm hơn gấp 2 lần
task.wait(5)
sound.PlaybackSpeed = 1 -- mặc địnhPlaying
Mẫu mã
Phát Âm Thanh
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("đang phát âm thanh")
sound.Playing = true
task.wait(10)
print("dừng âm thanh")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("tiếp tục tại vị trí thời gian: " .. tostring(timePosition)) -- c. 10 giâySoundId
Sound.SoundId:ContentId
TimeLength
Mẫu mã
Phát Âm trong Một Thời Gian Xác Định
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
Mẫu mã
Thời gian Âm thanh
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 = 0Phương Pháp
Pause
Sound:Pause():()
Lợi Nhuận
()
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0pause
Play
Sound:Play():()
Lợi Nhuận
()
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0play
Resume
Sound:Resume():()
Lợi Nhuận
()
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0Stop
Sound:Stop():()
Lợi Nhuận
()
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0stop
Sự Kiện
DidLoop
Mẫu mã
Lặp một số lần
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
-- ngắt kết nối
connection:Disconnect()
-- dừng âm thanh
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
Tham Số
Mẫu mã
Tải Âm Thanh
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("Âm thanh đã được tải!")Paused
Tham Số
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0Played
Tham Số
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0Resumed
Tham Số
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0Stopped
Tham Số
Mẫu mã
Các Hàm Âm Thanh
-- tạo một âm thanh
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://9120386436"
if not sound.IsLoaded then
sound.Loaded:Wait()
end
-- lắng nghe các sự kiện
sound.Played:Connect(function(_soundId)
print("Sự kiện Sound.Played")
end)
sound.Paused:Connect(function(_soundId)
print("Sự kiện Sound.Paused")
end)
sound.Resumed:Connect(function(_soundId)
print("Sự kiện Sound.Resumed")
end)
sound.Stopped:Connect(function(_soundId)
print("Sự kiện Sound.Stopped")
end)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0
task.wait(10)
sound:Pause()
print("tạm dừng", sound.Playing, sound.TimePosition) -- tạm dừng false 10
task.wait(3)
sound:Resume()
print("phát", sound.Playing, sound.TimePosition) -- phát true 10
task.wait(3)
sound:Stop()
print("dừng", sound.Playing, sound.TimePosition) -- dừng false 0
task.wait(3)
sound:Play()
print("phát", sound.Playing, sound.TimePosition) -- phát true 0