Background music

  • #1, by ke4Tuesday, 19. May 2015, 16:01 5 years ago
    Hi,

    i have music on background in one scene and the thing is that everytime i leave the scene and come back again the music is playing from the beginning, which is a bit disturbing,
    is it possible to start the music from random position?

    thanks

    Key Killer

    810 Posts


  • #2, by afrlmeTuesday, 19. May 2015, 16:42 5 years ago
    Only if you use the lua sound functions to start / stop / pause audio files. The files have to be loaded & controlled via Lua script to be able to use the openAL audio functions. Sounds started via the editor or via action parts can't be edited by Lua.

    Check out the various sound functions via this page.

    Imperator

    6842 Posts

  • #3, by ke4Tuesday, 19. May 2015, 17:13 5 years ago
    Guess something like this should work, but the sound is playing about 0.1 second only ( from the beginning )
    The folder podklady is in the same folder as Vis file.

    local soundID = getSoundId("vispath:podklady/Hudba/hotel/party.ogg")
    local soundOffset = getSoundProperty(soundID, "offset")
    startSound("vispath:podklady/Hudba/hotel/party.ogg", {flags=1, volume=40, offset=math.random(0, soundOffset) })
    1
    2
    3
    4


    Edit: i changed "offset" to "duration" and still the same.

    Key Killer

    810 Posts

  • #4, by afrlmeTuesday, 19. May 2015, 17:27 5 years ago
    Offset is the current playtime position. You want to use the duration property for the getSoundProperty() function.
    local soundOffset = getSoundProperty(soundID, "duration")
    1
    2


    P.S: you don't have to add 0, to the math.random function. By default if you only enter 1 value, then it will automatically return a number between 0 & the number you specified. The min number only has to be set if you want the min number to be something other than 0; say -10 or 22 for example.

    Imperator

    6842 Posts

  • #5, by ke4Tuesday, 19. May 2015, 17:36 5 years ago
    Thanks, i used the duration property, but it's the same result.
    So i tried to just play the sound without any properties and it still plays the sound just for a few ms.

    Okej, got it :-)

    Key Killer

    810 Posts

  • #6, by afrlmeTuesday, 19. May 2015, 17:50 5 years ago
    Ah you mean it's not playing the entire track? Try adding loop = true parameter. Also not 100% sure but I think you aren't actually generating the soundID. I think you need to use the getSoundId() function & that the sound needs to be playing to return an id.

    If you know the duration of the track in seconds then just google: "convert seconds to ms" & it will display a unit converter for time. Just enter in time in seconds or minutes to convert to milliseconds then add that to your math.random() number.

    http://www.checkyourmath.com/convert/time/milliseconds_secon...

    The math is pretty simple anyway...

    * seconds to milliseconds: seconds x 1000
    * milliseconds to seconds: milliseconds ÷ 1000

    120s = 120,000 milliseconds.

    Imperator

    6842 Posts

  • #7, by ke4Tuesday, 19. May 2015, 18:21 5 years ago
    Yeah i was just thinking how to do it more dynamically...
    It starts play the sound and after a few ms it stops.

    Current, without the first part:
    startSound("vispath:podklady/Hudba/hotel/party.ogg", {flags=1, volume=40, loop=true, offset=math.random(76000) })
    1
    2


    but nothing confuse

    Key Killer

    810 Posts

  • #8, by afrlmeTuesday, 19. May 2015, 18:37 5 years ago
    Are there any errors listed in the log file after executing the startSound() function?

    Imperator

    6842 Posts

  • #9, by ke4Tuesday, 19. May 2015, 18:40 5 years ago
    Nope, no errors.
    btw what the flags=1 means?

    Key Killer

    810 Posts

  • #10, by afrlmeTuesday, 19. May 2015, 18:44 5 years ago
    To be honest, I'm not really sure. grin
    I think it's just something that tells the engine that it should also update the table for the specified object / function with the included parameters.

    There are a few functions that seem to include the flags=1 thing.

    Imperator

    6842 Posts

  • #11, by ke4Tuesday, 19. May 2015, 18:49 5 years ago
    Hehe,

    anyway it's strange, i tried different file and path too and still the same, i'll leave it for now and get back to it later..
    Thanks

    Key Killer

    810 Posts

Write post