Two questions about the stop and resume the background music

  • #1, by wimfWednesday, 17. February 2016, 13:40 8 years ago

    I have two question about the stop and resume the background music

    In my room 1, I put a radio that broadcasts background music.

    I use this music via the "Background music" function (this is important because I use lua script that allows me to reduce the volume of my music when the character speaks)

    If the player turns off the radio, the music stop (with stop Currently playing sound)

    But, if the music continues to play where the player go in the next room, I will wish that the music continues without interruption (I just change my volume of the music to give the impression that sound is coming from the other room)

    So I search :

    1) how to start and stop a background music in lua
    2) how to make a "continue music from the previous scene" in lua

    Thank in advance

    Forum Fan

    238 Posts

  • #2, by afrlmeWednesday, 17. February 2016, 16:00 8 years ago
    I believe I actually wrote a script a while ago for controlling the background music for someone else - don't remember who though, or when - ah wait... scrap that. I wrote it for Simon_ASA for his Catyph / ASA games I think.

    It's not quite what you are asking for though as we wrote it to randomly assign an audio file out of a table to the background music for each scene & to play until the track ended & then it would automatically unload the background music file & call a pause function with a random time interval to delay between playing the next random background audio file. In the script we also checked at the end of a scene if a background music file was currently assigned & if so then we set the next scenes background file to be the same one, thus allowing the music to continue playing seamlessly into the next scene.

    Long story short... avoid using the "continue music from previous scene" option as it's (for lack of a better word) shit. What you want to do is manually add the same file as the background music sound. The engine will automatically continue playing the music into the next scene if it detects that it is the same file.

    Quick note #1: you can not pause & resume background music files or sounds started via the editor in general. The only way to dynamically control the playback position is through sounds started via the Lua startSound() function, however they will not continue seamlessly between scenes & will most likely quit when changing scene.

    game.CurrentScene.BackgroundMusic = "relative_path_to_sound/some_file.ogg" -- set a new background sound.
    game.CurrentScene:clearLink(VSceneBackgroundMusic) -- remove background sound (set empty)

    Quick note #2: If you wanted the music to appear as if it is continuing then you could simply mute or fade the scene background music volume in or out using Lua script & the to() tweening function.

    game.CurrentScene:to(100, {MusicVolume = 0}) -- fade music volume for current scene out over 100ms
    game.CurrentScene:to(100, {MusicVolume = 100}) -- music volume for current scene back in over 100ms

    Anyway... hope this all helps a wee bit.

    P.S: sorry for the long post.


    7278 Posts

  • #3, by wimfWednesday, 17. February 2016, 16:59 8 years ago
    Hello. Thank you

    I'm very happy, it's working :p you are the best !

    Forum Fan

    238 Posts

  • #4, by afrlmeWednesday, 17. February 2016, 17:49 8 years ago
    No problem at all mate. wink


    7278 Posts