Page 1 of 1
[Solved] Calc Macro to play an MP3 Audio File.
Posted: Tue Nov 27, 2012 12:10 am
by alf50
I am writing a game in OpenOffice Calc and would like to play various MP3 Sounds when the user wins with a certain score level. Anyone have a Macro to trigger a specific MP3 Audio file? I tried inserting an MP3 Audio file, Then using record macro to see what happened when I clicked the PLAY arrow when I high lighted the Audio Icon, but the macro never recorded commands for selecting the Icon, nor clicking the Play botton that appeared at the bottom.
Also, The INSERT Audio-Video command just adds a link to the MP3 File. If I throw out the MP3 file, the button no longer works. I would like to attach the MP3 Audio files to my Calc Workbook, so the sounds would play and I would not have to provide the separate MP3 files along with my application.
Re: Calc Macro to play an MP3 Audio File.
Posted: Tue Nov 27, 2012 4:49 pm
by hanya
I have made a tetris like game on Calc with simple sound effect and I used the following macro to play wav file:
Code: Select all
Sub InitSounds()
If GetGuiType() = 1 Then
oSounMgr = CreateUnoService("com.sun.star.media.Manager_DirectX")
Else
oSounMgr = CreateUnoService("com.sun.star.media.Manager_GStreamer")
End If
If NOT IsNull(oSounMgr) Then
oSfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
sBaseURL = CreateUnoService("com.sun.star.util.PathSubstitution")._
substituteVariables("$(inst)/share/gallery/sounds", True)
sSound1 = sBaseURL & "/laser.wav"
If oSfa.exists(sSound1) Then
oPlayer1 = oSounMgr.createPlayer(sSound1)
'oPlayer1.setRate(1.1)
oPlayer1.setPlaybackLoop(False)
oPlayer1.setMediaTime(0.0)
oPlayer1.setVolumeDB(GetSoundVolume())
End If
End If
End Sub
Sub PlayJoin()
If NOT IsNull(oPlayer1) Then
oPlayer1.setMediaTime(0.0)
oPlayer1.start()
End If
End Sub
I do not know the manager name for Mac OS X environment but it would be found in the list of availabled service names. And see API doc for css.media.Manager service about the service.
Re: Calc Macro to play an MP3 Audio File.
Posted: Wed Aug 19, 2015 10:15 pm
by czellweger
Thanks for the code. Surprisingly, on my Ubuntu 15.04 and LibreOffice 4.4.2.2., the UNO call had changed:
oSounMgr = CreateUnoService("com.sun.star.comp.media.Manager_GStreamer")
old: com.sun.star.media.Manager_GStreamer
You wonder how I found that: There were some code comparisons old-new in Google, where these changed definitions were reported.
No other documentation available whatsoever.
Didn't check the Windows version though.
It seems that Apache is working on a new XPlayer, yet "unpublished". No Idea how to get that one to work in OO or Libreoffice. Anybody has ?
https://www.openoffice.org/api/docs/com ... layer.html
Furthermore, I streamlined the code for my purposes.
Instead of the bInit boolean, I loop the procedure through a DoEvents while the music is playing and then destroy the objects.
I also inverted the checks and implemented some warnings instead of just quitting - file check first, otherwise it is useless to create the player.
The setMediaTime does not seem to have any effect. Surprisingly, the setVolumeDB (found in Xplayer) does work! Miracles happen.
So here we go:
Code: Select all
REM ***** BASIC *****
option explicit
Sub Main
PlaySound("/home/archeo5/Music/arusha-findings/Drakies/Drakensberg Boys_ Choir-2009 _Hamba Nathi _.mp3")
End Sub
Sub PlaySound(i_soundpath as string)
dim oPlayer1 as object
dim sUrlSound as string
dim oSounMgr as object
' if bInit then
if not isnull(oSounMgr) then
S_Start_New
exit sub
endif
' sUrlSound = F_get_Sound_URL("MySound.mp3")
sUrlSound = "file://" & i_soundpath
If not fileexists(sUrlSound) Then
msgbox sUrlSound & " does not exist",16
else
If GetGuiType() = 1 Then
oSounMgr = CreateUnoService("com.sun.star.media.Manager_DirectX")
Else
'.comp. was not documented !!
oSounMgr = CreateUnoService("com.sun.star.comp.media.Manager_GStreamer")
' com.sun.star.media.Manager_GStreamer
End If
If IsNull(oSounMgr) Then
msgbox "Sound Mgr not set",16
else
oPlayer1 = oSounMgr.createPlayer(sUrlSound)
oPlayer1.setMediaTime(0.0)
oPlayer1.setVolumeDB(-10)
' msgbox oPlayer1.getduration()
oPlayer1.setPlayBackLoop( 0 )
oPlayer1.start(0)
while oPlayer1.isplaying()
doevents
wend
oPlayer1 = nothing
oSounMgr = nothing
msgbox "sound ended",16
End If
End If
End Sub
Re: Calc Macro to play an MP3 Audio File.
Posted: Mon May 16, 2016 9:10 pm
by VBWizard
I've tried using czellweger 's code above (with a different MP3), and I get an "Object variable not set" error in each of the following lines:
oPlayer1.setMediaTime(0.0)
oPlayer1.setVolumeDB(-10)
oPlayer1.setPlayBackLoop( 0 )
oPlayer1.start(0)
while oPlayer1.isplaying()
why is this ?
Re: Calc Macro to play an MP3 Audio File.
Posted: Tue May 17, 2016 12:06 am
by musikai
The url handling in the code is only for linux. Better replace this line:
Code: Select all
sUrlSound = "file://" & i_soundpath
with:
Code: Select all
sUrlSound = ConvertToUrl(i_soundpath)
Full edited code:
Code: Select all
Sub Main
PlaySound("E:\Musik\Allemande Buxtehude.mp3")
End Sub
Sub PlaySound(i_soundpath as string)
dim oPlayer1 as object
dim sUrlSound as string
dim oSounMgr as object
' if bInit then
if not isnull(oSounMgr) then
S_Start_New
exit sub
endif
' sUrlSound = F_get_Sound_URL("MySound.mp3")
sUrlSound = ConvertToUrl(i_soundpath)
If not fileexists(sUrlSound) Then
msgbox sUrlSound & " does not exist",16
else
If GetGuiType() = 1 Then
oSounMgr = CreateUnoService("com.sun.star.media.Manager_DirectX")
Else
'.comp. was not documented !!
oSounMgr = CreateUnoService("com.sun.star.comp.media.Manager_GStreamer")
' com.sun.star.media.Manager_GStreamer
End If
If IsNull(oSounMgr) Then
msgbox "Sound Mgr not set",16
else
oPlayer1 = oSounMgr.createPlayer(sUrlSound)
oPlayer1.setMediaTime(0.0)
oPlayer1.setVolumeDB(-10)
' msgbox oPlayer1.getduration()
oPlayer1.setPlayBackLoop( 0 )
oPlayer1.start(0)
while oPlayer1.isplaying()
doevents
wend
oPlayer1 = nothing
oSounMgr = nothing
msgbox "sound ended",16
End If
End If
End Sub
Re: Calc Macro to play an MP3 Audio File.
Posted: Tue May 17, 2016 8:52 pm
by VBWizard
That's fixed it. Many thanks Musikai