Hello,
I am having an issue with roblox’s new AudioPlayer. The issue is that when a sound usually reaches around the halfway point, the song stops playing and the TimePosition value resets.
I have revised my code several times and tried numerous fixes but im starting to assume this is an internal error with the audio player. However knowing me, Its probably a code error that ive constantly been overlooking. as i am still not completly familliar with the audio api.
This is the code attached below:
-- starting api
task.wait(10) -- wait for server start to prevent odd bug
local current_Announcer = nil
local USERCONF = require(script.Parent.Parent.Parent.CONFIG)
local SPKRNET = script.Parent.SPKR_NETWORK
local CurrentAudioIDPlaying = nil
local CurrentAudioTableKeyNUM = nil
local LastPlayedAudioID = nil
local IsPlaying = false
local BMSFOLDER = script:FindFirstAncestor("ASSETS").Parent
local API:BindableEvent = BMSFOLDER:WaitForChild("OKBMS_API")
local audioPlayer = script:WaitForChild("Player")
local fader = script:WaitForChild("Fader")
local APlayers = {}
if not _G.OKBMS then
_G.OKBMS={}
end
_G.OKBMS["Audio_Player"]=audioPlayer
_G.OKBMS["Audio_Fader"]=fader
-- rolloff mechanics
local MIN_DISTANCE = 10
local MAX_DISTANCE = 45
local CURVE_STEP_SIZE = 2
local voiceCurve = {}
for i = MIN_DISTANCE, MAX_DISTANCE, CURVE_STEP_SIZE do
voiceCurve[i] = ((i - MIN_DISTANCE) - (MAX_DISTANCE - MIN_DISTANCE))^2 / (MAX_DISTANCE - MIN_DISTANCE)^2
end
--voiceCurve[MAX_DISTANCE] = 0
--main
function PlaySong(SongFullData)
-- audioPlayer:pla
warn("PLAY")
--audioPlayer.TimePosition=0
--audioPlayer.IsPlaying=false
audioPlayer.AssetId="rbxassetid://"..SongFullData["ID"]
repeat wait() until audioPlayer.IsReady
audioPlayer:Play()
warn(audioPlayer.TimeLength)
task.wait(audioPlayer.TimeLength)
warn("DONE")
end
task.spawn(function()
do
for i,v in pairs(script.Parent:GetChildren()) do
warn(tostring(i).." = "..tostring(v))
if not v:IsA("BindableEvent") then
if v:FindFirstChild("SoundPart") then
local sp=v:FindFirstChild("SoundPart")
local Emitter = Instance.new("AudioEmitter")
local wire=Instance.new("Wire")
Emitter.Parent=sp
Emitter:SetDistanceAttenuation(voiceCurve)
wire.Parent=Emitter
wire.SourceInstance=fader
wire.TargetInstance=Emitter
table.insert(APlayers,Emitter)
end
end
end
_G.OKBMS["AudioEmmiters"]=APlayers
end
end)
-- loop
warn("DOLOOP")
task.spawn(function()
while true do
if USERCONF["SOUNDSYSTEM"]["SHUFFLE"] then
local NextAudio = USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"][math.random(1,#USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"])]
--local ShuffleAttempts = 0
if LastPlayedAudioID ~= nil then
warn(LastPlayedAudioID)
if LastPlayedAudioID == NextAudio["ID"] then
warn("MATCHED, SHUFFLING")
repeat
NextAudio = USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"][math.random(1,#USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"])]
warn(LastPlayedAudioID)
warn(NextAudio["ID"])
--ShuffleAttempts = ShuffleAttempts + 1
--[[if ShuffleAttempts == 5 or ShuffleAttempts > 5 then
warn('OKBMS :: UNABLE TO FIND A DIFFERENT AUDIO FOR MUSIC SHUFFLE, REPLAYING LAST PLAYED SONG')
break
end]]
until LastPlayedAudioID ~= NextAudio["ID"]
PlaySong(NextAudio)
--audioPlayer.Ended:Wait()
else
PlaySong(NextAudio)
--audioPlayer.Ended:Wait()
end
else
PlaySong(NextAudio)
--audioPlayer.Ended:Wait()
end
elseif not USERCONF["SOUNDSYSTEM"]["SHUFFLE"] then
if CurrentAudioTableKeyNUM == #USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"] then
CurrentAudioTableKeyNUM = 0
end
local NextAudio = USERCONF["SOUNDSYSTEM"]["AUDIO_LIST"][CurrentAudioTableKeyNUM+1]
PlaySong(NextAudio)
--audioPlayer.Ended:Wait()
end
end
end)
-- fix errors
-- UI SYNC ERROR
audioPlayer.Changed:Connect(function()
if type(audioPlayer.AssetId) == "string" then
local CID = tonumber(string.sub(audioPlayer.AssetId,14,string.len(audioPlayer.AssetId)))
warn(CID)
script.CURRENT_ID.Value = CID
LastPlayedAudioID = CID
CurrentAudioIDPlaying = CID
else
script.CURRENT_ID.Value = tonumber(audioPlayer.AssetId)
LastPlayedAudioID = tonumber(audioPlayer.AssetId)
CurrentAudioIDPlaying = tonumber(audioPlayer.AssetId)
end
end)
-- PAUSE ERR
SPKRNET.Event:Connect(function(call,args)
if call == "pauseGlobal" then
script.AUDIO_PAUSED.Value = true
elseif call == "resumeGlobal" then
script.AUDIO_PAUSED.Value = false
end
end)
-- player sync
for i,v in pairs(game.Players:GetPlayers()) do
local loader = script:WaitForChild("Local_Listener"):Clone()
repeat wait() until v.Character
loader.Parent=v.Character
loader.Enabled=true
end
game.Players.PlayerAdded:Connect(function(p)
local loader = script:WaitForChild("Local_Listener"):Clone()
repeat wait() until p.Character
loader.Parent=p.Character
loader.Enabled=true
end)
-- announcement
fader.Volume = USERCONF["SOUNDSYSTEM"]["VOLUME"]
function end_announcement()
current_Announcer=nil
for i,v in pairs(APlayers) do
if v:FindFirstChildWhichIsA("Wire") then
v:FindFirstChildWhichIsA("Wire").SourceInstance = fader
end
end
fader:WaitForChild("Wire").SourceInstance = audioPlayer
API:Fire("Register_Annoucement_End")
end
API.Event:Connect(function(prot,param)
if prot=="Start_Annoucement" then
API:Fire("Register_Annoucement")
if param["player"] == nil then return end
if current_Announcer~=nil then return end
local player:Player = game.Players:FindFirstChild(tostring(param["player"]))
player.CharacterRemoving:Connect(end_announcement)
current_Announcer = player
fader:WaitForChild("Wire").SourceInstance = script:WaitForChild("AnnouncementChime")
script:WaitForChild("AnnouncementChime"):Play()
task.wait(script:WaitForChild("AnnouncementChime").TimeLength)
for i,v in pairs(APlayers) do
if v:FindFirstChildWhichIsA("Wire") then
v:FindFirstChildWhichIsA("Wire").SourceInstance = player:FindFirstChildWhichIsA("AudioDeviceInput")
end
end
elseif prot=="End_Annoucement" then
end_announcement()
end
end)
The code is part of my BMS system, this code selects a random audio and plays it, it should not be resetting the audioPlayer randomly which further strengthens my assumption that it is an issue with the AudioPlayer instance.
Any help is appreciated, Thank You!