我在我的Django应用程序中使用audiofield来允许上传.wav文件并将其渲染为播放。
我能够一次成功实现一次上传,向玩家显示单个上传的文件
代码是:
def bottom_player(self):
if self.a_file:
file_url = settings.MEDIA_URL + str(self.a_file)
Html5_string = '<div style="position:fixed; top:5em; right:5em; height: 5em; background-color:white;"><audio controls><source src="%s" type="audio/mpeg">%s Your browser does not support the audio element.</audio><br><h3>Audio player will adjust to your screen</h3></div>' % (file_url, os.path.basename(self.a_file.name))
return Html5_string
bottom_player.allow_tags = True
现在,我想为多个上传文件显示单独的播放器,因此我创建了另一个表,其中包含用于上载的a_file字段和一个SQL触发器,用于将新创建的a_file字段值以逗号分隔格式复制到上一个a_file列
audio-file-NACHY-1698934831.wav,。/ audio-file-ABGYT-1890930931.wav
要解析此a_file列值,然后为每个路径显示播放器,代码如下所示:
def bottom_player(self):
if self.a_file:
audiolist = self.a_file
filenamelist = audiolist.split(",")
filenamelist
for filename in filenamelist:
file_url = settings.MEDIA_URL + str(filename)
Html5_string += '<div style="position:fixed; top:5em; right:5em; height: 5em; background-color:white;"><audio controls><source src="%s" type="audio/mpeg">%s Your browser does not support the audio element.</audio><br><h3>Audio player will adjust to your screen</h3></div>' % (file_url, os.path.basename(self.filename))
return Html5_string
bottom_player.allow_tags = True
我在Django页面的底部播放器字段中显示“无”。