【发布时间】:2019-01-12 14:13:51
【问题描述】:
我的本地 Web 应用程序(使用 java spark 框架)创建了一个 Html5 报告,其中一些页面包含可以播放的音频文件。
最初这完全是通过 Html 完成的,例如
<audio controls="controls">
<source src="/Music/Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\02 - The Slave's Lament.WAV">
</audio>
但它仅在音乐位于 Web 应用程序根文件夹的子文件夹中时才有效。所以为了解决这个问题,我创建了一个指向根文件夹的符号链接(/Music 是 webservers 目录中指向 / 的符号链接)
但是符号链接在 WINdows 上不可用,而在 UNIX 上,符号链接会导致另一个工具出现问题,所以我正在寻找另一种方法。
现在我正在尝试使用服务器端点,因为所有文件对服务器都是可见的
<audio controls="controls">
<source src="/fixsongs.play_music?url=E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\01 - Peg and Awl.WAV">
</audio>
play_music 端点方法是这样的
public byte[] playUrl(Request request, Response response)
{
String filename = request.queryParams("url");
MainWindow.logger.severe("playMusic:"+filename);
try
{
if (filename != null)
{
Path path = Paths.get(filename);
if (Files.exists(path))
{
byte[] data = Files.readAllBytes(path);
MainWindow.logger.severe("playMusic:"+filename+":" + data.length);
return data;
}
}
}
catch(Exception ex)
{
MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return null;
}
现在这种方法可行,但存在许多问题。
我希望 play_music url 仅在我实际单击控件上的播放时才被调用,但实际上它在我一开始就为前 6 个文件调用它 打开网页,此日志输出证明了这一点
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\01 - Peg and Awl.WAV
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\02 - The Slave's Lament.WAV
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\03 - Gilmartin.WAV
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\04 - Jackaro.WAV
05/08/2018 11.53.38:BST:CmdRemote:lambda$start$90:SEVERE: >>>>>/fixsongs.play_music
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\06 - Calling My Children Home.WAV
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\05 - Once I Knew A Pretty Girl.WAV
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\01 - Peg and Awl.WAV:31265996
05/08/2018 11.53.38:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\02 - The Slave's Lament.WAV:36671026
05/08/2018 11.53.39:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\03 - Gilmartin.WAV:50752138
05/08/2018 11.53.39:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\04 - Jackaro.WAV:46483668
05/08/2018 11.53.39:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\06 - Calling My Children Home.WAV:33175954
05/08/2018 11.53.39:BST:ServerFixSongs:playUrl:SEVERE: playMusic:E:\Melco\TestMusic\TestMusic\WAV\Music\David Ferrard\Across The Troubled Wave\05 - Once I Knew A Pretty Girl.WAV:44547648
为什么要这么做?
可以播放这些文件,但所有文件的总曲目长度设置为相同的错误值。
可播放文件的渲染错误(将前六个与其余文件进行比较)
其他文件都无法播放
这是使用 Wav 文件,然后我尝试了文件的 mp3 版本,现在它加载了所有文件并且音轨长度正确。所以我想这是一个资源问题,但是这个应用程序将部署在一个服务大多数 wav 文件的慢速机器上,所以它不能在页面显示后立即尝试加载所有文件,所以有没有只有在用户想要播放它们时才加载它们的方式,因为在大多数情况下,它们实际上并不想播放任何东西。
【问题讨论】:
-
啊找到了我需要将 preload="none" 添加到音频标签的预加载答案。我不知道如何修复 Wavs 不正确的轨道长度,但只有当 wavs 从服务器返回时才不正确,如果我只是使用旧的文件 url 方法就可以了。
标签: html html5-audio spark-java