Music is, to put it bluntly, a bit messed up and tricky to work in
WC3. So here's my music system, taken out of Destiny's Wake (with a few alterations, mainly taking out the need for Vex's CS safety). Obviously it still needs vJASS. Will store where it is in the track
library MusicSystem
initializer Setup_Music
globals private string current =
null private timer t
constant string FIGHT_MUSIC =
"3circles.mp3" constant string NORMAL_MUSIC =
"Medieval.mp3" private real fightTime = 0.
private real normalTime = 0.
endglobals// These must have the data of the various songs you use.private function SetMusicRestart
takes string s
returns nothing if s == FIGHT_MUSIC
then set fightTime = ModuloReal
(fightTime + TimerGetElapsed
(t
), 620.
) elseif s == NORMAL_MUSIC
then set normalTime = ModuloReal
(normalTime + TimerGetElapsed
(t
), 245.
) endifendfunction// This must have the correct returns for the various songs you use.private function GetMusicRestart
takes string s
returns real if s == FIGHT_MUSIC
then return fightTime
elseif s == NORMAL_MUSIC
then return normalTime
else return 0.
endifendfunctionprivate function StartMusicEx
takes nothing returns nothing call StopMusic
(false) if current !=
null then call PlayMusic
(current
) call SetMusicPlayPosition
( R2I
( GetMusicRestart
(current
)*1000
) ) call TimerStart
(t, 99999.,
false,
null) endif call DestroyTimer
(GetExpiredTimer
())endfunctionfunction StartMusic
takes string s
returns nothing call ClearMapMusic
() if s !=
null then call SetMapMusic
(s,
false, 0
) endif if current !=
null then call StopMusic
(true) call SetMusicRestart
(current
) if s !=
null then call TimerStart
(CreateTimer
(), 2.,
false,
function StartMusicEx
) // This time is to allow time for fade out. Reduce it to make the new music come in faster. endif else call StopMusic
(false) if s !=
null then call PlayMusic
(s
) call SetMusicPlayPosition
( R2I
( GetMusicRestart
(s
)*1000
) ) call TimerStart
(t, 99999.,
false,
null) endif endif set current = s
endfunctionprivate function Setup_Music
takes nothing returns nothing set t = CreateTimer
()endfunctionendlibrary
How to use it? Quite simple. Set up the two functions which I've commented, and the necessary globals, and then whever you want to use it, do this:
call StartMusic(NORMAL_MUSIC) //this will play the normal music variable
call StartMusic(null) // This will stop the music