Advertisement
Hendrix000007

Usingbackground Music

Feb 4th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. ---------------------------------------------------
  2. -- MusicLib.lua
  3. ---------------------------------------------------
  4. local _M = {}
  5.  
  6. local audioChannel
  7. local otherAudioChannel
  8. local currentSong
  9. local curAudio
  10. local prevAudio = 1
  11.  
  12.  
  13. audio.crossFadeBackground = function (path, force)
  14.         local musicPath = path
  15.         if currentSong == musicPath and audio.getVolume{channel = audioChannel} > 0.1 and not force then return false end
  16.         audio.fadeOut({channel=audioChannel, time=1000})
  17.         if audioChannel==1 then audioChannel,otherAudioChannel=2,1 else audioChannel,otherAudioChannel=1,2 end
  18.         audio.setVolume( 1, {channel = audioChannel})
  19.         curAudio = audio.loadStream( musicPath )
  20.         audio.play(curAudio, {channel=audioChannel, loops=-1, fadein=1000})
  21.         prevAudio = curAudio
  22.         currentSong = musicPath
  23.         audio.currentBackgroundChannel = audioChannel
  24. end
  25. audio.reserveChannels(2)
  26. audio.currentBackgroundChannel = 1
  27.  
  28. return _M
  29.  
  30.  
  31.  
  32. -------------------------------------------------------
  33. -- ANY FILE YOU LIKE THIS FUNCTION IN:
  34. -------------------------------------------------------
  35. require ( "MusicLib" )
  36.  
  37. audio.crossFadeBackground("yourMusicFile.mp3", 1)
  38.  
  39. -- IF YOU WANT THIS TO WORK IN I.E. COMPOSER OR STORYBOARD, JUST PUT THE LINE IN ENTERSCENE LIKE:
  40. --STORYBOARD:
  41. function scene:enterScene( event )
  42.     local screenGroup = self.view
  43.     audio.crossFadeBackground("yourMusicFile.mp3", 1)
  44. end
  45.  
  46. -- COMPOSER:
  47. function scene:show( event )
  48.    local sceneGroup = self.view
  49.    local phase = event.phase
  50.    if ( phase == "will" ) then
  51.         audio.crossFadeBackground("yourMusicFile.mp3", 1)
  52.    elseif ( phase == "did" ) then
  53.    end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement