Advertisement
Guest User

music.lua

a guest
Feb 9th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1.  
  2. function widget:GetInfo()
  3.   return {
  4.     name      = "Music",
  5.     desc      = "Plays music",
  6.     author    = "zwzsg",
  7.     date      = "Aug 22 2009",
  8.     license   = "Free",
  9.     layer     = 0,
  10.     enabled   = true
  11.   }
  12. end
  13.  
  14. local AllTracks      = {}
  15. local CurrentTrack   = nil
  16. local info           = ""
  17. local LastTimer      = nil
  18. local LastPlayedTime = 0
  19. local PlayNew        = true
  20.  
  21. function widget:Initialize()
  22.   for _,track in ipairs(VFS.DirList('music/'..Game.modShortName..'/','*.ogg')) do
  23.     table.insert(AllTracks,track)
  24.   end
  25.   for _,track in ipairs(VFS.DirList('music/','*.ogg')) do
  26.     table.insert(AllTracks,track)
  27.   end
  28. end
  29.  
  30. local function JustTheName(text)
  31.   local EndIndex=(string.find(text,".",string.len(text)-4,true) or 1+string.len(text))-1
  32.   local BeginIndex=1
  33.   repeat
  34.     local NewBeginIndex=string.find(text,"/",BeginIndex,true) or string.find(text,"\\",BeginIndex,true)
  35.     BeginIndex=NewBeginIndex and NewBeginIndex+1 or BeginIndex
  36.   until not NewBeginIndex
  37.   return string.sub(text,BeginIndex,EndIndex)
  38. end
  39.  
  40. function widget:Shutdown()
  41.   Spring.StopSoundStream()
  42. end
  43.  
  44. function widget:Update()
  45.   local PlayedTime, TotalTime = Spring.GetSoundStreamTime()
  46.   if Spring.IsDevLuaEnabled() then
  47.     info = "PlayedTime="..(PlayedTime or "nil").."\nTotalTime="..(TotalTime or "nil")
  48.     info = (CurrentTrack and (JustTheName(CurrentTrack).."\n") or "")..info
  49.   end
  50.   PlayedTime=PlayedTime or 0
  51.   TotalTime=TotalTime or 0
  52.   if not LastTimer then
  53.    LastTimer=Spring.GetTimer()
  54.    return
  55.   end
  56.   local Timer=Spring.GetTimer()
  57.   if Spring.DiffTimers(Timer,LastTimer)>2 and (PlayedTime>=TotalTime-0.1 or PlayedTime==0) then
  58.     LastTimer=Timer
  59.     if LastPlayedTime==PlayedTime then
  60.       PlayNew=true
  61.     else
  62.       LastPlayedTime=PlayedTime
  63.     end
  64.   end
  65.   if PlayNew then
  66.     if not AllTracks  or #AllTracks<1 then
  67.       Spring.Echo("No music found!   Copy some .ogg into /music/")
  68.       widgetHandler:RemoveWidget()
  69.     else
  70.       local x,y=Spring.GetMouseState()
  71.       local BetterRand=x+y+math.floor(99*(os.clock()%99999)+(99*(os.time())%99999))+Spring.GetDrawFrame()+math.random(0,999)
  72.       CurrentTrack = AllTracks[1+(BetterRand%#AllTracks)]
  73.       --Spring.SetConfigInt("snd_volmusic",100) -- Neither
  74.       Spring.StopSoundStream()
  75.       Spring.PlaySoundStream(CurrentTrack,0.3) -- Volume here does nothing
  76.       Spring.Echo("Music: "..JustTheName(CurrentTrack))
  77.       PlayNew=false
  78.     end
  79.   end
  80. end
  81.  
  82. function widget:DrawScreen()
  83.   if Spring.IsDevLuaEnabled() then
  84.     gl.Text(info,0,150,16)
  85.   end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement