Skip to content
Advertisement

Playing a wavefile from the html using flask framework?

This is my run.py code

JavaScript

This my base.html file

JavaScript

this my tasks.html

JavaScript

This my s1.html

JavaScript

In the s1.html i have a hyperlink to a audio wav file . I want to play the audio file from the s1.html. The wav file is present in the root directory.
It is throwing error

GET /ps.wav HTTP/1.1″ 404 Please tell how to solve this. Help will be highly appreciated!

Advertisement

Answer

In the case of audio files, when you give <audio src="C://somePath"/>, this throws an error saying cannot load local resource. This makes sense because any webpage can’t simply give a local path and access your private files.

In case you are trying to play audio with dynamic paths, by changing src property through JS, then here is a sample implementation using Flask server and HTML.

server.py

JavaScript

audioMap.html

JavaScript

Explanation:

When you give the audio file name under the src property, this creates a get request in the flask as shown

JavaScript

As you can see that flask sent a Get request for the xyz.wav file. So to serve this get request, we wrote an endpoint that takes the audio file name, reads it from the local directory, and returns it back. Hence the audio shows up on UI.

Note: This works only if you are rendering your HTML file using the render_template method via flask or to say, using flask as your web server.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement