简体   繁体   中英

Audio not playing from Python script from server

I am facing a peculiar issue here. I am trying to load all the audio links to my Python script from a json file so that they can be played client side, they display correctly but after the 4th nothing plays - I press the play button but there is no audio.

I have the path of several sound files stored in a json file (what I am trying to load is much bigger ~50-60 urls) but here is an example:

file.json:

{

    "Music Type 1" : "http://music/file1.mp3",
    "Music Type 2" : "http://music/file2.mp3",
    "Music Type 3" : "http://music/file3.mp3",
    "Music Type 4" : "http://music/file4.mp3",
    "Music Type 5" : "http://music/file5.mp3"
}

And to display these on my webpage I am using the following:

#!C:/Network/Python/Scripts/python.exe
print("Content-Type: text/html\n")

import json
a = json.load(open('file.json'))

for i in a.keys():
    print('<h1>{}</h1>\n'.format(i))
    print('<audio controls><source src="{}"></audio>'.format(a[i]))

Everything displays correctly when viewing source (browser side):

<h1>Music Type 1</h1>

<audio controls><source src="http://music/file1.mp3"></audio>
<h1>Music Type 2</h1>

<audio controls><source src="http://music/file2.mp3"></audio>
<h1>Music Type 3</h1>

<audio controls><source src="http://music/file3.mp3"></audio>
<h1>Music Type 4</h1>

<audio controls><source src="http://music/file4.mp3"></audio>
<h1>Music Type 5</h1>

<audio controls><source src="http://music/file5.mp3"></audio>

My first thoughts that the urls were wrong, so I checked the url of Music Type 5 and it was correct. It played from browser as expected.

I then tried a second Python script with the same url and it played perfectly:

#!C:/Network/Python/Scripts/python.exe
print("Content-Type: text/html\n")

print('<audio controls><source src="http://music/file5.mp3"></audio>')

For those who do not know how to use the audio tag here is a quick reference. I am running XAMPP server Windows. The files are also live streams.

I do not understand why the audio is not playing. Can anyone tell me how to fix this, or what could be causing this problem?

It turns out that the mp3 files were corrupt, the browser did not recognise them to play. The script itself works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM