Two API: PlayMusic() StopMusic()
Using the "music" track for addon sounds? i.e. set all to 25% and this to 100% to distinct certain events.
Prototype:
lua Code:
local frames = {}
local function Create()
local f
for i=1, 10 do -- create 10 frames
f = CreateFrame("Frame")
f:Hide()
f:SetScript("OnShow", function(self)
if not self.sound or not self.len then return end
SetCVar("musicVolume", self.volume or 1)
StopMusic()
PlayMusic(self.sound)
end)
f:SetScript("OnUpdate", function(self, elapsed)
if not self.sound or not self.len then return end
self.elapsed = (self.elapsed or 0) + elapsed
if self.elapsed >= self.len then
StopMusic()
self.sound = nil
self.len = nil
self.volume = nil
self.elapsed = nil
self:Hide()
end
end)
table.insert(frames, f)
end
end
local function GetAvailable()
for _,f in pairs(frames) do
if not f.sound and not f.len then
return f
end
end
end
local function GetSoundFileLength(sound)
local len = 0
-- NYI
return len
end
local function PlaySoundFile2(sound, volume)
local f = GetAvailable()
f.sound = sound
f.len = GetSoundFileLength(sound)
f.volume = volume
f:Show()
end
local function SetVolumeSettings()
-- Init. volume settings so the music channel trick works and that normal music is not playing
end
Using PlaySoundFile2("1.mp3", 0.5) will store the sound, len and volume int othe frame and Show it. The onShow will from there set the music volume cvar, stop the last sound and play the current one. The OnUpdate will handle stopping the music when the sound expires, clear the temp variables and hide the frame to avoid OnUpdate performance instability.
The only problem is the GetSoundFileLength part as the game has no way of knowing how long a sound is, but for things like announcements one could run the game where sound effects and such are on 25% while the music is at 100% (and turn off the in-game music so it does not bother you). Now when a sound is played on the music channel it's louder than any other sound. Only instability is that it loops if not stopped (it is in this addon) and that it can not play multiple music files at once so it's pointless for anything but announcements that do not occur too often -perhaps something Bossmods should incorporate.