none
How to play a sound loop using Timer? RRS feed

  • Question

  • Hi all,

    How do I play custom sounds in a Timer callback Sub without crashing (a sound loop, for example)?  I can play built-in sounds using Timer just fine but there seems to be some thread ownership issue using custom files in V1.3.

    The calling thread cannot access this object because a different thread owns it.

       at System.Windows.Threading.Dispatcher.VerifyAccess()
       at System.Windows.Freezable.WritePreamble()
       at _SmallBasicProgram.ontimer()
       at Microsoft.SmallBasic.Library.Timer.ThreadTimerCallback(Object tag)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.TimerQueueTimer.CallCallback()
       at System.Threading.TimerQueueTimer.Fire()
       at System.Threading.TimerQueue.FireNextTimers()

    Thanks,
    --BKB

    Friday, October 19, 2018 7:53 AM

Answers

  • Please see threads:

    My custom sounds won't repeat

    repeating background music

    and/or Technet Article

    add background music to a Small Basic game

    and/or ID: QLM846 (LitDev's Turtle Maze Game)  from Challenge Jan2015

    Friday, October 19, 2018 8:33 AM
    Answerer
  • I managed to work it out with this minimalist example:

    mySound = Program.Directory + "\Alarm.mp3" ' 1-second sound clip
    Timer.Interval = 3000 '  3-second interval
    Timer.Tick = onTimer

    Sub onTimer '   Play Alarm  every 3 seconds
      Timer.Pause() ' not thinking to pause the timer onTimer was my mistake
      While "True"
        Sound.PlayAndWait(mySound) ' also, PlayAndWait in a timer callback seems odd
      EndWhile
    EndSub

    Just also pointing out inconsistent behavior when using built-in Sounds:

    Timer.Interval = 3000 '  3 second interval
    Timer.Tick = onTimer
    Sub onTimer ' Play Chime  every 3 seconds
      Sound.PlayChime()
    EndSub

    Thanks Pappa!
    --BKB



    • Marked as answer by Brian Broker Friday, October 19, 2018 1:55 PM
    • Edited by Brian Broker Friday, October 19, 2018 2:33 PM Provided example of simplicity.
    Friday, October 19, 2018 1:55 PM

All replies

  • Please see threads:

    My custom sounds won't repeat

    repeating background music

    and/or Technet Article

    add background music to a Small Basic game

    and/or ID: QLM846 (LitDev's Turtle Maze Game)  from Challenge Jan2015

    Friday, October 19, 2018 8:33 AM
    Answerer
  • Hmm... so the solution is a bit convoluted, it seems.  Perhaps some safeguards could be implemented for beginners who don't like to see a stack trace when trying something intuitive.
    Friday, October 19, 2018 1:14 PM
  • I managed to work it out with this minimalist example:

    mySound = Program.Directory + "\Alarm.mp3" ' 1-second sound clip
    Timer.Interval = 3000 '  3-second interval
    Timer.Tick = onTimer

    Sub onTimer '   Play Alarm  every 3 seconds
      Timer.Pause() ' not thinking to pause the timer onTimer was my mistake
      While "True"
        Sound.PlayAndWait(mySound) ' also, PlayAndWait in a timer callback seems odd
      EndWhile
    EndSub

    Just also pointing out inconsistent behavior when using built-in Sounds:

    Timer.Interval = 3000 '  3 second interval
    Timer.Tick = onTimer
    Sub onTimer ' Play Chime  every 3 seconds
      Sound.PlayChime()
    EndSub

    Thanks Pappa!
    --BKB



    • Marked as answer by Brian Broker Friday, October 19, 2018 1:55 PM
    • Edited by Brian Broker Friday, October 19, 2018 2:33 PM Provided example of simplicity.
    Friday, October 19, 2018 1:55 PM