Advertisement
Hendrix000007

MusicLib

Feb 11th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. --------------------------------------
  2. MUSIC LIB CLASS (MusicLib.lua)
  3. --------------------------------------
  4.  
  5. local _M = {}
  6.  
  7.  
  8. local audioChannel
  9. local otherAudioChannel
  10. local currentSong
  11. local curAudio
  12. local prevAudio = 1
  13.  
  14. audio.crossFadeBackground = function (path, force)
  15.         local musicPath = path
  16.         if currentSong == musicPath and audio.getVolume{channel = audioChannel} > 0.1 and not force then return false end
  17.         audio.fadeOut({channel=audioChannel, time=1000})
  18.         if audioChannel==1 then audioChannel,otherAudioChannel=2,1 else audioChannel,otherAudioChannel=1,2 end
  19.         audio.setVolume( 1, {channel = audioChannel})
  20.         curAudio = audio.loadStream( musicPath )
  21.         audio.play(curAudio, {channel=audioChannel, loops=-1, fadein=1000})
  22.         prevAudio = curAudio
  23.         currentSong = musicPath
  24.         audio.currentBackgroundChannel = audioChannel
  25. end
  26. audio.reserveChannels(2)
  27. audio.currentBackgroundChannel = 1
  28.  
  29. return _M
  30. -------------------
  31. usage in i.e main.lua
  32. --------------------
  33. local mL = require ( "MusicLib" )
  34. audio.crossFadeBackground("bgSplashSound.mp3")   --dont work....ahhh carrrrramba!! I dont get it!!
  35.                           -- Its written in a way I don't write
  36.                           --so I get confused
  37.                           --Is this a private method for internal
  38.                           --handling in my class?
  39.  
  40. --[[
  41. --------------------------------------
  42. Here are the original MusicLib Class I wrote earlier that works partially
  43. --------------------------------------
  44.  
  45. local _M = {}    
  46.     gemSound = {[1]="blopSound2.mp3", [2]="failSound.mp3", [3]="blopBlopSound.mp3"}
  47.     bgSound = {[1]="bgSplashSound.mp3", [2]="failSound.mp3", [3]="blopBlopSound.mp3"}
  48.    
  49.     _M.blopSound = audio.loadSound(gemSound[1])
  50.     _M.failSound = audio.loadSound(gemSound[2])
  51.     _M.blopBlopSound = audio.loadSound(gemSound[3])
  52.    
  53.     function _M:bgMusic(levelTrack)
  54.         local fChannel = audio.findFreeChannel ()
  55.         _M.backgroundMusic = audio.loadStream(bgSound[levelTrack])
  56.         audio.play(_M.backgroundMusic, { loops=-1, channel= fChannel })
  57.     end
  58.    
  59.     function _M:bgMusicFadeOut()
  60.         audio.fadeOut({ time=500 } )
  61.         audio.dispose(backgroundMusic)
  62.         audio.dispose(blopSound)
  63.         audio.dispose(failSound)
  64.         audio.dispose(blopBlopSound)
  65.         _M.backgroundMusic = nil
  66.         _M.blopSound = nil
  67.         _M.failSound = nil
  68.         _M.blopBlopSound = nil
  69.         return true
  70.     end
  71.  
  72. return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement