Play a sound when a form button is pressed

Greatings. I´m not trying to make a piano board, I’m just trying to play a simple sound, like laser.mp3, when I press a button to open another form. This seems to be quite easy but for me is a difficult start. I think this macro should prevent the sound file to play indefinitely in loops. If you want to suggest me some written articles I’ll be glad. Thank you.

No macro required.

Open your Form document in the Design/Edit mode

Right click on the Push Button Control and select Control Properties

On the General tab of the Properties: Push Button dialog scroll down to Action

Select the Open Document/web page option.

On the URL line just below select the box with the ellipsis to open the Filer Picker.

Select the audio file you want to play.

Exit the Design/Edit mode

Now when you push the button is should play your selection using your default player. Most players have options for single play or looping.

I’m sorry I didn´t express my needs effectively. When I push the button it runs already an action that opens another form, so I can’t have two actions in the same button. What I think I have to do is to add more code to the macro:

Sub OpenFormVisitors
ThisDatabaseDocument.FormDocuments.getByName(“frm_Visitors”).Open
End Sub

Something that makes my “click.wav” sound in the background, at the same time the form opens. Can it be done with LOB? Thank you, any way.

Maybe check this old macros…

https://forum.openoffice.org/en/forum/viewtopic.php?t=57699

Some hints also here:

Well, thank you sir for your time but i do not wish to create a music box, i just need to add a sound click to a button when pressed without any interaction with the player. In fact i don’t want to see it, but only listen to the click sound in the background.

You didn’t reveal your operating system or type of database you are using

This code is from a Windows 10 computer using an Embedded HSQL database

A macro to play a sound requires a link to the audio player, and a link to the audio file

then a call to the shell

sAudioPlayer = "C:\Program Files\VideoLAN\VLC\vlc.exe"

sAudioLink = ConvertToURL(“F:\My Documents\AudioClips\Ding.wav”)

Shell(sAudioPlayer,1,sAudioLink,false)

Thank you for your time but I probably made something wrong because when I press the action button the sound is heard and the form opens. Unfortunately the VLC interface page and icon stay on the screen and I have to close it to see the form behind it. It seems that LOB does not accept “false” in the call.

Sub AbrirFormVisitantes
sAudioPlayer = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
sAudioLink = ConvertToURL("C:\Users\vquar\Documents\Base de Dados\sons\click-1.wav")
Shell(sAudioPlayer,1,sAudioLink,false)
ThisDatabaseDocument.FormDocuments.getByName("frm_Visitantes").Open
End Sub

Regards

The second argument in the Shell() command can be changed . Perhaps one of those will be helpful, although in my tests the only one that has any noticeable effect it 10

0 Focus is on the hidden program window.
1 Focus is on the program window in standard size.
2 Focus is on the minimized program window.
3 Focus is on the maximized program window.
4 Standard size program window, without focus.
6 Minimized program window, but focus remains on the active window.
10 Full-screen display.

By default VLC opens with the same size window as last used.
If you manually shrink the VLC window so it is smaller than your form and close it
When the macro runs, you can select the form even while the audio clip is playing.

VLC has a Play and exit option.
Press Ctrl>P in VLC to get the Preferences dialog.
In the lower left under Show Setting select the option button All
Scroll down the list and select Playlist
check Play and exit
Save

When the clip finishes VLC closes.

Well, it seems that I have to approach the problem some other way. If LOB doesn’t answer to my needs it’s ok, it’s not the end of the world if I can’t play a sound in the background at the same time I open a form without having to interact with or see the player. Thank you for trying to help me. All the best!

Find a command line player for your operating system.

Well, thank you for your useful and enlightening comment. I will!

I just tried on Linux with shell("/usr/bin/play", 2, ConvertToURL("/tmp/test/aperture.wav")) but the code fails silently without sound nor error.

P.S. Of course, the convertToURL is counter productive since the called sound player does not swallow file urls. shell("/usr/bin/play", 2, "/tmp/test/aperture.wav") plays the sound when I call it from the Basic editor but remains silent when the active window is a document window. I tried all window constants (2nd argument) with no success.

One can try options to vlc -I null --play-and-exit as suggested here:

In my opinion I think you’re trying to mix different things. That does not aplly to Libre Office or any other Data Base, it’s for the computer OS. But I may be wrong.