存储在服务器上的示例歌曲(Sample song stored on server)

编程入门 行业动态 更新时间:2024-10-25 20:23:03
存储在服务器上的示例歌曲(Sample song stored on server)

我正在设计一个网站,允许用户在决定是否购买之前收听30秒的歌曲样本。 我计划将所有mp3存储在根文件夹下,以便不能直接访问它们。

我想弄清楚的是,是否有一种方法可以通过利用完整的mp3文件来听到30秒的歌曲样本,或者我是否必须将已经剪切的样本mp3存储在服务器也是。 谢谢。

I'm designing a site that would allow users to listen to a 30 second sample of a song before deciding whether or not to purchase it. I plan on having all the mp3s stored below the root folder so that they cannot be directly accessed.

What I'm trying to figure out though, is whether there is a way to allow a 30 second sample of the song to be heard by utilizing the full mp3 file, or if I'll have to have an already cut sample mp3 stored on the server as well. Thanks.

最满意答案

下面的代码应该是您正在寻找的:

HTML:

<audio src="song.mp3" id="song">

使用Javascript:

var song = document.getElementById('song'); song.play(); //Start playing sound setTimeout(function(){song.pause(), 30000}; //Pause the song after 30,000 millisends(30 seconds);

要么

var song = document.getElementById('song'); song.play(); //start playing sound setTimeout(function(){song.src="";}, 30000); //Instead of pausing the song, you remove the source file of the song which basically stops the sound

第二个选项可能更可取,因为任何具有基本HTML / JS知识的人都可以轻松地取消暂停音频文件并收听整首歌曲。 但是,您最好的选择是修剪服务器上的歌曲,因为它可以节省宝贵的带宽。 可以使用命令行音频编辑工具sox( http://sox.sourceforge.net/ )使用以下语法轻松完成

sox <originalFile> <newFile> trim <startTime> <duration>

例:

sox input.wav output.wav trim 0 00:35

我最近一直在生产网站上使用sox,这非常有用! 大多数服务器端框架必须能够生成可以运行诸如此类命令的子进程。

希望这可以帮助 :)

编辑:在阅读Solai的评论后,我认为如果您使用PHP并希望在服务器端进行操作,那么他链接的解决方案将是最简单的。 如果你想在客户端或不使用PHP的情况下这样做,那么我的可能就是(现在)。

The code below should be what you're looking for:

HTML:

<audio src="song.mp3" id="song">

Javascript:

var song = document.getElementById('song'); song.play(); //Start playing sound setTimeout(function(){song.pause(), 30000}; //Pause the song after 30,000 millisends(30 seconds);

or

var song = document.getElementById('song'); song.play(); //start playing sound setTimeout(function(){song.src="";}, 30000); //Instead of pausing the song, you remove the source file of the song which basically stops the sound

The second option would probably be preferable because anyone with basic HTML/JS knowledge could easily unpause the audio file and listen to the entire song. However, your best option would be to trim the song on the server because it saves precious bandwidth. It can easily be done with the command line audio editing tool sox(http://sox.sourceforge.net/) using the following syntax

sox <originalFile> <newFile> trim <startTime> <duration>

Example:

sox input.wav output.wav trim 0 00:35

I have been using sox on a production site lately and it's very useful! Most server-side frameworks have to ability to spawn a subprocess that can run commands such as this one.

Hope this helps :)

EDIT: After reading the comment, I think the linked solution would be the simplest solution if you're using PHP and want to do it server-side. If you want to do it client-side or you're not using PHP, my solution would probably be the way to go.

更多推荐

本文发布于:2023-08-07 02:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   器上   歌曲   Sample   stored

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!