The reason of this problem:
When we releasing music stream, SMBX doesn't send own commands like "Pause/Resume" to MCI proxy to SDL, and when we switching with Alt+Tab, music continues playing.
My code example (I was set random music trigger), it switching musics when it needed and doesn't stop music playback when you click Alt+Tab.
Code: Select all
-- Music Box, will play each music file from playlist. Switching automatically after 20 sec.
-- Timer
TickTack = 0
-- Music track
CurTrack = 1
-- playlist
Tracks = {
"i_bach_joke.it",
"schwing.mod",
"Double_Cherry_Pass.it",
"ice_ow.mid",
"AliBaba_4Cumbia.mp3",
"the_reincarnation_of_yammah.s3m",
"backtoth.mod",
"8bitenized.xm",
"monkey_island_v1.xm",
"D_MESSAG.it"
}
-- Play SFX
function playSwitchSnd()
playSFXSDL("pswitch.ogg")
end
function switchMusic()
if(TickTack==0)then
CurTrack = math.random(table.getn(Tracks))
-- Open sound track and store
MusicOpen(Tracks[CurTrack])
-- Set timeout 20 sec.
TickTack=1300
playSwitchSnd()
MusicVolume(127)
else
TickTack=TickTack-1
end
-- Start playback and resume if paused
if(TickTack==130)then
MusicStopFadeOut(2000)
else
if(TickTack<=1172)then
MusicPlay()
else
if(TickTack==1298)then
MusicVolume(127)
MusicPlay()
end
end
end
printText(Tracks[CurTrack], 10, 10);
end
function onLoop()
end
-- Play all custom musics in the second section
function onLoadSection1(playerIndex)
if(playerIndex==1) then
-- Open current track to overwrite previouse music file
MusicOpen(Tracks[CurTrack])
end
end
-- Do loop in second section
function onLoopSection1(playerIndex)
if(playerIndex==1) then
switchMusic()
end
end
function onLoad()
TickTack=0
end
My solution of problem is:
Start level from section which has the default SMBX's music ID (even any custom music)
Second section with "None" music which will be used for LUA music player.
You can even make dummy section with any default music and with instant warp which will teleport character into musical section. When we started level playthrough, SMBX will still send pause() command even if music "none", but when we started level from "none" music, SMBX will don't send music commands.
Also, music stops when you entered into another section with "none" music and after alt+tab. If you doesn't manually stopped previous music or if you don't started one of default SMBX musics, last LunaLUA's music will continue playing before you will start any default SMBX music or before you will switch Alt+Tab
P.S. By anyway I will make the "Audio" namespace in the LUA and I will put into them more functions than musicOpen(), musicPlay(), musicStop(), etc.