[solved] How to play an mp3 from macro?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

[solved] How to play an mp3 from macro?

Post by DorinDXN »

Hi everyone,

I've tried winsound (with wav instead), looks like is working but but produces no sound.
then I've tried without success to install playsound module and pygame module,
again, without success :( pip simply not execute anything.

Is a simple way to play an mp3 from a macro?

thanks!

cheers,
Dorin
Last edited by DorinDXN on Sun Jul 24, 2022 6:54 am, edited 1 time in total.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to play an mp3 from macro?

Post by openBrain »

You can install PySide2 QtMultimedia package and use appropriate classes of it.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to play an mp3 from macro?

Post by heda »

https://pypi.org/project/playsound/
single file - so could even copy instead of installing

at least that was the top result when searching the web...
or if that does not work there is always a system call to any audio player possible...
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to play an mp3 from macro?

Post by DorinDXN »

Thanks for replies, here are my findings so far

Code: Select all

import winsound
winsound.PlaySound("S:\\Sound\\1.wav", winsound.SND_FILENAME) # OK
winsound.PlaySound("S:\\Sound\\1.wav", winsound.SND_ASYNC) # OK
#wav only, works both only in Python 3.8.10

Code: Select all

from playsound import playsound
playsound("S:\\Sound\\1.mp3", True) # OK
playsound("S:\\Sound\\1.mp3", False) # Not Working
#works in Python  2.7.14 and Python 3.8.10, only 1st, not working in ASYNC mode, with 2nd param False

Code: Select all

from PySide2 import QtMultimedia
QtMultimedia.QSound.play("S:\\Sound\\1.wav") # no sound
# no sound, reports :  using null output device, none available
for now I have to use winsound
unfortunately is not supported in FreeCAD 0.18 - Python 2.7.14
I'd preferred as FreeCAD 0.20 has full-screen issues on my PC (Win 7)
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to play an mp3 from macro?

Post by DorinDXN »

BTW, any way to have winsound working on FreeCAD 0.18 Python 2.7.14 :) ?
DorinDXN
Posts: 66
Joined: Sat Oct 31, 2020 8:07 am

Re: How to play an mp3 from macro?

Post by DorinDXN »

Solved for FreeCAD 0.18 Python 2.7.14 Win 7 :)

copied mp3play folder from here
https://github.com/michaelgundlach/mp3play
in the folder with macros
then made voiceplay() function and works both sync and async ,
tested with this code

Code: Select all

import time
from threading import Thread
import mp3play
def voiceplay(filename, async):
	if async :
		thread = Thread(target=voiceplay, args=(filename, False))
		thread.start()
	else:
		voice = mp3play.load(filename)
		voice.play()
		time.sleep(voice.seconds())

voiceplay("S:\\Sound\\1.mp3", True) #async
voiceplay("S:\\Sound\\3.mp3", False)
cheers,
Dorin
Post Reply