Back to Home

ESO Lua File v100023

libraries/utility/zo_queuedsoundplayer.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
ZO_QueuedSoundPlayer = ZO_Object:Subclass()
local g_id = 1
--[[ Public API ]]--
function ZO_QueuedSoundPlayer:New(...)
    local queuedSoundPlayer = ZO_Object.New(self)
    queuedSoundPlayer:Initialize(...)
    return queuedSoundPlayer
end
function ZO_QueuedSoundPlayer:Initialize(soundPaddingMs)
    self.queue = {}
    self.soundPaddingMs = soundPaddingMs or 0
    self.id = "ZO_QueuedSoundPlayer" .. g_id
    g_id = g_id + 1
    self.OnUpdateFunction = function() self:OnSoundFinished() end
end
end
function ZO_QueuedSoundPlayer:PlaySound(soundName, soundLength)
    if self:IsPlaying() then
        self.queue[#self.queue + 1] = { soundName = soundName, soundLength = soundLength }
    else
        self:StartSound(soundName, soundLength)
    end
end
function ZO_QueuedSoundPlayer:ForceStop()
    EVENT_MANAGER:UnregisterForUpdate(self.id)
    self.currentPlayingSoundLength = nil
end
function ZO_QueuedSoundPlayer:IsPlaying()
    return self.currentPlayingSoundLength ~= nil
end
--[[ Private API ]]--
function ZO_QueuedSoundPlayer:StartSound(soundName, soundLength)
    self.currentPlayingSoundLength = soundLength + self.soundPaddingMs
    PlaySound(soundName)
    EVENT_MANAGER:RegisterForUpdate(self.id, self.currentPlayingSoundLength, self.OnUpdateFunction)
end
function ZO_QueuedSoundPlayer:OnSoundFinished()
    EVENT_MANAGER:UnregisterForUpdate(self.id)
    if #self.queue > 0 then
        local nextSound = table.remove(self.queue, 1)
        self:StartSound(nextSound.soundName, nextSound.soundLength)
    else
        self.currentPlayingSoundLength = nil
        if self.finishedAllSoundsCallback then
            self.finishedAllSoundsCallback(self)
        end
    end
end