Length of a Sound

I have a situation where the user can play a sound file using Sound.Play. What I want to do is enable a Stop button if the sound is longer than about 15 seconds. Is there a way to tell how long a sound will play? Right now, I’m starting a 15 second timer when the sound starts and enabling a Stop button if the timer fires and Sound.IsPlaying is still true. Is that the right approach or is there a better way?

Well, it works - just not great. The user has to wait for your 15 second timer before they can stop the sound; but they might want to stop the sound before then. In my opinion the user should be able to stop the sound as soon as it starts playing regardless of the length.

If you use Movie Player to play the sound you can get the Duration out of that, but if I recall correctly you can’t get the Duration on Windows unless the media is playing.

Concur with @Tim Parnell ; use a movie player so you can get the length of the sound and provide a GUI earlier on in playback or make the movieplayer gui visible to the user so they have more control ovee the sound playback.

Yeah, after a bit of thought, and testing, I came to the same conclusion. It certainly makes the coding much easier. So now, I enable the Stop button as soon as the sound starts playing and change it to ‘Replay’ if the sound has stopped.

If you’re still interested in getting the length of a song, I wrote a little cross-platform routine to do it on MacOS and Windows. (With thanks to Eugene Daiken and his book on Windows Declares!)

GetMediaLength

[quote=477487:@Andy Broughton]If you’re still interested in getting the length of a song, I wrote a little cross-platform routine to do it on MacOS and Windows. (With thanks to Eugene Daiken and his book on Windows Declares!)

GetMediaLength[/quote]
Thanks. I’ll take a look at it.

Length of a Sound ?

After running the shared project, I realized that this conversation is about the duration of songs… :frowning:

Why am I complaining ? I already found, some days ago, the Value of a Label (it is not 2 bucks, but it is a Caption String !).

That is the new XojoGP language.

[quote=477487:@Andy Broughton]If you’re still interested in getting the length of a song, I wrote a little cross-platform routine to do it on MacOS and Windows. (With thanks to Eugene Daiken and his book on Windows Declares!)

GetMediaLength[/quote]
Nifty little utility, Andy. Since I redesigned my code, i don’t need it any more but I certainly will keep it in my tool box. If I did use it, I would have to rewrite it since all the sound files my app would be looking at are .wav files.

Right, I think I was only interested in .mp3 files.

Just change

Dim f As FolderItem = GetOpenFolderItem("*.mp3")

in PushButton1.Action to

Dim f As FolderItem = GetOpenFolderItem("*.wav")

or whatever filetype you’re interested in. I know the same functions work for .wav files too and likely most media file types.

[quote=477681:@Andy Broughton]Right, I think I was only interested in .mp3 files.

Just change

Dim f As FolderItem = GetOpenFolderItem("*.mp3")

in PushButton1.Action to

Dim f As FolderItem = GetOpenFolderItem("*.wav")

or whatever filetype you’re interested in. I know the same functions work for .wav files too and likely most media file types.[/quote]
Actually it doesn’t. The SendString method tosses an error 259. I didn’t investigate it any further.

Oh, interesting! I did try it before so I’m not sure what the issue is.