Tms91
Tms91

Reputation: 4214

How to play a sound saved in a known directory with Django, via python script (not template)?

I am developing an app in Django. I have an mp3 file saved in the directory my_project/static/sounds/1607.mp3

I have a script.py file located in my_project/my_app/my_folder How can I tell my Django to play it at the end of a script.py?

Upvotes: 1

Views: 951

Answers (2)

Tms91
Tms91

Reputation: 4214

You can do it with playsound module.

This is a (inefficient) way to indicate Django the directory in which is the file you want to play:

from playsound import playsound
import os    
from django.contrib.staticfiles import finders

result = finders.find('static/sounds/1607.mp3')
searched_locations = finders.searched_locations
sound_dir = os.path.join(searched_locations[0]+r'\sounds\1607.mp3')

playsound(sound_dir)

print("Sound just played!")

Upvotes: 0

Srijwal R
Srijwal R

Reputation: 573

Is it ok for you to save the mp3 in a model as FileField ? By this way, you can store the file in the required location using ' upload_to ' too.

And more specific answer to your question: You can try VLC. Hope this helps

Upvotes: 1

Related Questions