How to stop playsound() before it starts playing

  • When you play a sound file, for instance, a vocal description when popping up an image (eg. playsound(tour, "something.mp3"), it doesn't actually start playing until the file is fully loaded into cache. What if the viewer leaves the event/popup that called the sound file before it starts playing? You don't want to have it start a second or two later. stopsound(tour) doesn't work since the sound hasn't started. destroysound(tour) causes an error since the sound doesn't exist, yet, apparently. I tried to supersede the unplayed sound with another using the same id, such as playsound(tour, "silence.mp3") but that didn't work.

    So, how do you stop a sound before it starts paying?

    Note: streamsound() may be an option but unless there is a solution to the above, you have to conclude that playsound() can almost never be safely used. Preloading all the sound files isn't a great solution. Say you have 50 voice-over files and which is played first is entirely random. If the viewer picks one that hasn't been preloaded the wait before starting could be even worse, by my understanding.

  • klaus.krpano November 2, 2023 at 11:12 AM

    Added the Label Checking
  • Several updates and in the end I seem to have found a solution, but not a great on.

    I don't think the preloading idea would work. From what I can tell there is no way to know if the preloading has finished. As I recall I could do that using javascript but that would make this whole issue more complicated.

    Klaus - I wonder if adding a sound[name].onloaded event might be worthwhile. Perhaps consider having stopsound() cancel the downloading of a sound file if it hasn't completed, yet.

    The solution was to play another sound in the same-named sound object. So, while sound[tour] is still downloading playsound(tour, "silence.mp3") prevents the downloading file from playing. I don't think the downloading is actually stopped because a little later an error gets thrown, "ERROR: Soundinterface Load Error: Decoding audio data failed." If that is correct, the old downloading file will continue to hog bandwidth that won't be available to download a new audio file. A way to actually cancel the download would be better. Forcing a user on a slow connection to sit idle while an unwanted download finishes before they can play a different sound file is not a good experience.

    Silence.mp3 is a one-second mp3 of silence.

    Originally I said that I tried the above and it didn't work. That was when I called an action in my soundplayer plugin to play silence.mp3. Although it includes the same playsound() action, that didn't work. Just calling playsound() by itself did, although apparently only by throwing an error.

    BTW, you can tell if the file is still downloading by checking sound[name].position. If it isn't > 0 it hasn't started playing because it hasn't finished downloading.

    Why has no developer had this problem before? I think it is because we all have fast systems with fast internet so we wouldn't notice it. Not everyone is so lucky.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!