Calc Macro to play an MP3 Audio File.

Creating a macro - Writing a Script - Using the API

Calc Macro to play an MP3 Audio File.

Postby alf50 » Tue Nov 27, 2012 12:10 am

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.
OpenOffice 4.1.5 on Mac El Capitan(10.11.6) and MS Windows10
alf50
 
Posts: 97
Joined: Sun Jun 13, 2010 2:55 pm

Re: Calc Macro to play an MP3 Audio File.

Postby hanya » Tue Nov 27, 2012 4:49 pm

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   Expand viewCollapse view
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.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Calc Macro to play an MP3 Audio File.

Postby czellweger » Wed Aug 19, 2015 10:15 pm

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   Expand viewCollapse view
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
Ubuntu 15.04 LibreOffice 4.4.2.2.
czellweger
 
Posts: 1
Joined: Wed Aug 19, 2015 9:52 pm

Re: Calc Macro to play an MP3 Audio File.

Postby VBWizard » Mon May 16, 2016 9:10 pm

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 ?
OpenOffice 4.1.2 on Windows XP
VBWizard
 
Posts: 8
Joined: Sat May 14, 2016 5:05 pm

Re: Calc Macro to play an MP3 Audio File.

Postby musikai » Tue May 17, 2016 12:06 am

The url handling in the code is only for linux. Better replace this line:
Code: Select all   Expand viewCollapse view
     sUrlSound = "file://" & i_soundpath


with:

Code: Select all   Expand viewCollapse view
     sUrlSound = ConvertToUrl(i_soundpath)


Full edited code:
Code: Select all   Expand viewCollapse view
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
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 228
Joined: Wed Nov 11, 2015 12:19 am

Re: Calc Macro to play an MP3 Audio File.

Postby VBWizard » Tue May 17, 2016 8:52 pm

That's fixed it. Many thanks Musikai
OpenOffice 4.1.2 on Windows XP
VBWizard
 
Posts: 8
Joined: Sat May 14, 2016 5:05 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 13 guests