エンジンクラス
Sound
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
プロパティ
SoundId:ContentId |
イベント
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 |
コードサンプル
音楽再生パート
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)APIリファレンス
プロパティ
EmitterSize
IsLoaded
コードサンプル
サウンドを読み込む
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("サウンドが読み込まれました!")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) -- 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, trueIsPlaying
コードサンプル
サウンドが再生中とサウンドが一時停止中
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, trueisPlaying
Looped
コードサンプル
指定した回数だけループする
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
-- 接続を切断します
connection:Disconnect()
-- 音を停止します
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)MaxDistance
MinDistance
Pitch
PlaybackLoudness
コードサンプル
ボリューム振幅バー
-- StarterPlayer > StarterPlayerScripts に配置すること
local Players = game:GetService("Players")
-- ローカルプレイヤーのPlayerGuiを待つ
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- ScreenGuiを作成
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui
-- バーのホルダーを作成
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
-- バーを作成
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
-- サウンドを作成
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = screenGui
sound:Play()
-- 最大音量を定義
local maxLoudness = 30
-- 振幅バーをアニメート
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
bar.Size = UDim2.new(amplitude, 0, 1, 0)
wait(0.05)
endPlaybackSpeed
コードサンプル
サウンド 再生速度
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 -- デフォルトPlaying
コードサンプル
サウンド再生
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
sound.Looped = true
sound.Parent = workspace
print("サウンド再生中")
sound.Playing = true
task.wait(10)
print("サウンド停止中")
sound.Playing = false
task.wait(5)
sound.Playing = true
local timePosition = sound.TimePosition
print("再開時の時間位置: " .. tostring(timePosition)) -- 約 10 秒SoundId
Sound.SoundId:ContentId
TimeLength
コードサンプル
特定の期間サウンドを再生する
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
コードサンプル
サウンド時間位置
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方法
Pause
Sound: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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0pause
Play
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0play
Resume
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0Stop
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0stop
イベント
DidLoop
コードサンプル
指定した回数だけループする
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
-- 接続を切断します
connection:Disconnect()
-- 音を停止します
sound:Stop()
end
end)
sound:Play()
end
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://0"
loopNTimes(sound, 5)Loaded
パラメータ
コードサンプル
サウンドを読み込む
local sound = script.Parent.Sound
if not sound.IsLoaded then
sound.Loaded:Wait()
end
print("サウンドが読み込まれました!")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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0Played
パラメータ
コードサンプル
サウンド関数
-- サウンドを作成する
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0Resumed
パラメータ
コードサンプル
サウンド関数
-- サウンドを作成する
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0Stopped
パラメータ
コードサンプル
サウンド関数
-- サウンドを作成する
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 イベント")
end)
sound.Paused:Connect(function(_soundId)
print("Sound.Paused イベント")
end)
sound.Resumed:Connect(function(_soundId)
print("Sound.Resumed イベント")
end)
sound.Stopped:Connect(function(_soundId)
print("Sound.Stopped イベント")
end)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0
task.wait(10)
sound:Pause()
print("一時停止", sound.Playing, sound.TimePosition) -- 一時停止 false 10
task.wait(3)
sound:Resume()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 10
task.wait(3)
sound:Stop()
print("停止", sound.Playing, sound.TimePosition) -- 停止 false 0
task.wait(3)
sound:Play()
print("再生", sound.Playing, sound.TimePosition) -- 再生 true 0