Adding a sound affect to python code in VSC - python

I have been trying to add sound to my python code. I tried using playsound, pygame, pydub and tkinter but in vsc it doesn't let me install any using the command pip3 install pygame...(and so on)
here I have tried it with pydub but this sais that there is no module named pydub and it doesn't let me install it. Can someone help me add this sound to my code?
from pydub import AudioSegment
from pydub.playback import play
psong = AudioSegment.from_mp3("song.mp3")
play(song)

I have two suggestions for this issue:
Install pyaudio (pip3 install pyaudio) as this is the best module that pydub uses.
Change your audio file format to wav instead of mp3 and also change this section of code: psong = AudioSegment.from_mp3("song.mp3") to psong = AudioSegment.from_wav("song.wav").
Source for answer

Related

Python playsound() error when i change directory

I've a problem with Playsound and i don't know why... for me it's not understandable...
The code
from playsound import playsound
playsound(r"C:\\Users\\Toto\\Desktop\\songs\\dring.mp3")
It's work i'm happy, now i copy/paste the same directory (songs\dring.mp3) away from the desk for example my usb device (or other, no matter where i put it)
On my code i change the path
from playsound import playsound
playsound(r"D:\\songs\\ding.mp3)
i've always this error
Error 259 for command:
the driver cannot recognize the specified command parameter.
It's work only my desktop, elsewhere it does not work.. can you help me?
Thank
I found the solution...
you need to uninstall playsound 1.3.0 using :
pip uninstall playsound
(press y to proceed)
Then uninstall the old and pure version of playsound by this command :
pip install playsound==1.2.2
It worked perfectly for me

Unable to play sound even after installing playsound in python

Downloaded the sound from https://sounds-mp3.com/, and the syntax of the playsound was from the official site of it. Still there is no sound played and giving me the below mentioned output of execution.
from playsound import playsound
playsound('C:\\Users\\HP\\Downloads\\0003537.mp3')
output:
python -u "c:\Users\HP\Desktop\python files\hello.py"
I doubt it is a problem with the code, it looks fine.
It might be to do with your speakers or the audio file. Try out a wav and see if that works. Check your path too. I have had problems in the past with files downloaded from the web so maybe record something on Audacity and see if that plays. Try playing the audio file with the default software for your os, does it work?

Why is the playsound function not able to play my mp3 file in python

So basically I was trying to create a music playing program that would play music when you asked it to. I was just trying to get the song playing portion to actually work, so I wrote a super simple code to test it out:
import playsound
playsound('sample.mp3')
This was done in replit, and I added the mp3 file into the files section
when I hit run, it gives me TypeError: 'module' object is not callable. What can I do to fix this?
you need to install the PyObjC by writing below command.
if you are using python version 3
pip3 install PyObjC
if you are using python version 2
pip install PyObjC
After that you write below code in your terminal
from playsound import playsound
playsound("path of your audio file/testing_audio_file.mp3")
if you are facing any issue regarding path then use below pattern for (use two forward slash) Windows
playsound("D:\\path_of_your_audio_file\\testing_audio_file.mp3")
Replace
import playsound
with...
from playsound import playsound

Python is not playing sound but it finishes the program successfully without errors

I am trying to play sound in Python. When I run my code, it finishes successfully but the audio is not played by my computer.
I have tried with all the libraries like playsound, simpleaudio, pydub and kivy core audio.
Sample code I tried is:
from playsound import playsound
playsound('myfile.wav')
As i think it should work, check either your audio file (maybe it's empty) or check your audio drivers and audio output.
OR, You can try
pip install playsound
one more time
I've had this problem before, use the complete path:
from playsound import playsound
playsound(r"C:\some\path\sound.mp3") # the "r" is necessary, or else your code wont work!

using pydub to convert audio from .au to .wav

In this code m getting an error as follows:
import os
from pydub import AudioSegment
song = AudioSegment.from_file('C:/Users/Rishabh/Desktop/metal.00000.au','au')
song.export(path[:-3]+"wav",format='wav')
I am getting the following error:
WindowsError: [Error 2] The system cannot find the file specified
I realize your question is more than two and a half years old by now, and hopefully you've not been stuck with this problem since then. But maybe someone else fumbles into the same problem (such as me...)
As Jiaaro mentions, check the ffmpeg path. Also, make sure you've installed ffmpeg in your environment. Here is an old thread I found on github with some more detail in checking ffmpeg availability, also with answers from Jiaaro:
https://github.com/jiaaro/pydub/issues/5#issuecomment-8397126
Myself, I'm running anaconda on windows 10 and needed to install pydub and ffmpeg separately in my conda environment for pydub to work.
If your goal is to simply convert from au to wav (without any other programmatic context) you can execute ffmpeg in your environment from the terminal with the -i flag. Like so:
ffmpeg -i "C:\Users\Rishabh\Desktop\metal.00000.au" "C:\Users\Rishabh\Desktop\metal.00000.wav"

Categories

Resources