如何使用 php 播放 mp3 音乐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11327066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play a mp3 music using php?
提问by aF.
I've a directory on the site with several musics.
我在网站上有一个目录,里面有几首音乐。
What I want is to click on a simple button and listen to one of those musics.
我想要的是单击一个简单的按钮并聆听其中一首音乐。
Is this possible using php?
这可以使用php吗?
回答by user1204384
PHP is a server-side script that is used more for data processing and interacting with databases. What you're looking for is a client-side script, so using javascript or flash is the better way to play MP3's. There are plenty of free JS and Flash plugins for you to download or install on the internet.
PHP 是一种服务器端脚本,更多地用于数据处理和与数据库的交互。您正在寻找的是客户端脚本,因此使用 javascript 或 flash 是播放 MP3 的更好方法。有许多免费的 JS 和 Flash 插件供您在互联网上下载或安装。
My personal favorite is SoundManager2, http://www.schillmania.com/projects/soundmanager2/which is more of a development type API, for creating and customizing your own applications. It is by far one of the more flexible audio players for its incredibly small optimized builds.
我个人最喜欢的是 SoundManager2, http://www.schillmania.com/projects/soundmanager2/,它更像是一种开发类型的 API,用于创建和自定义您自己的应用程序。它是迄今为止更灵活的音频播放器之一,因为它的优化构建非常小。
回答by Raphael Pineda
There's this script generator in which you can be able to let your Web page play audio. Here is the link: http://www.scriptgenerator.net/44/Audio-player-script-generator/
有这个脚本生成器,您可以在其中让您的网页播放音频。这是链接:http: //www.scriptgenerator.net/44/Audio-player-script-generator/
回答by silentw
You can use HTML5:
您可以使用 HTML5:
var snd = new Audio("file.wav"); // buffers automatically when created
snd.play();
You can then use onclicks, etc and trigger the snd.play()
然后您可以使用 onclicks 等并触发 snd.play()
But looking at your question, I think that HTML5 doesn't support mp3...
但是看着你的问题,我认为 HTML5 不支持 mp3 ......
回答by PeeHaa
Of course this is not possible using PHP only (since PHP is serverside and I think you want to play clientside). However with either javascript / flash this can easily be done.
当然,仅使用 PHP 是不可能的(因为 PHP 是服务器端,我认为您想玩客户端)。但是使用 javascript / flash 这可以很容易地完成。
If you want to be cool (and don't have to support older user-agents) you could use html5's native audio.
如果你想很酷(并且不必支持旧的用户代理),你可以使用 html5's native audio。
I've had good fun with AudioManager 2which tries to use JS and falls back to flash.
我在尝试使用 JS 并回退到 Flash 的AudioManager 2上玩得很开心。
回答by Aman Virk
ok playing songs directly with php is not the right way or probably you will not even find a secure way to do that, there are some really good options over the net, i mean jquery plugin like jwPlayer and you can search more on google. Which can help you setting up a complete audio player with playlist. But again i am not sure what you have in your mind i have created a simple function that will help you playing songs.
好的,直接用 php 播放歌曲不是正确的方法,或者你可能甚至找不到安全的方法来做到这一点,网上有一些非常好的选择,我的意思是像 jwPlayer 这样的 jquery 插件,你可以在谷歌上搜索更多。这可以帮助您设置带有播放列表的完整音频播放器。但是我再次不确定您的想法我创建了一个简单的功能来帮助您播放歌曲。
<?php
function getUploadedFiles($directory){
$folder = opendir($directory);
while ($file = readdir($folder)) {
if($file !== '.' && $file !== '..')
{
$filename = $directory.'/'.$file;
$file_array[] = array('filename' => $file);
}
}
return $file_array;
}
$music = getUploadedFiles('music');
foreach($music as $value)
{
echo '<a href="music/'.$value['filename'].'">'.$value['filename'].'</a>';
}
?>
Function will mostly depend upon the browser or music player installed inside user machine, so it is never right to trust what user is going to have + your file path will be visible.
功能主要取决于用户机器中安装的浏览器或音乐播放器,因此相信用户将拥有什么+您的文件路径将是可见的永远是不对的。
I still recommend look for a stable jquery html5 plugin which supports flash as a fallback and you can easily setup a complete audio player with playlist.
我仍然建议寻找一个稳定的 jquery html5 插件,它支持 Flash 作为后备,您可以轻松地设置一个完整的音频播放器和播放列表。
回答by Purpletoucan
Media handling is managed by the browser. The best way to do this is to put out an HTML page from your PHP with audio embedded, see:-
媒体处理由浏览器管理。最好的方法是从你的 PHP 中发布一个嵌入音频的 HTML 页面,请参阅:-
http://www.w3schools.com/html/html_sounds.asp
http://www.w3schools.com/html/html_sounds.asp
Without embedding them properly in HTML, the browser will treat them as files for download and then it's up to each browser as to what helper it uses to play them, or whether they are just saved to disk.
如果没有在 HTML 中正确嵌入它们,浏览器会将它们视为供下载的文件,然后由每个浏览器决定它使用什么助手来播放它们,或者它们是否只是保存到磁盘。

