使用HTML在网站中嵌入媒体播放器[关闭]

问题描述 投票:12回答:6

我想要做的是在网站中嵌入音乐文件(在网站上有一些小小的MP3播放器)。我希望观众能够通过使用定制的控制器来播放,停止等歌曲。

如何编写这些自定义按钮以使它们都正常工作?

html embed audio-player
6个回答
28
投票

你可以使用很多东西。

  • 如果你是标准迷,你可以使用HTML5 <audio>标签:

Here is the official W3C specification for the audio tag

用法:

<audio controls>
 <source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4"
         type='audio/mp4'>
 <!-- The next two lines are only executed if the browser doesn't support MP4 files -->
 <source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga"
         type='audio/ogg; codecs=vorbis'>
 <!-- The next line will only be executed if the browser doesn't support the <audio> tag-->
 <p>Your user agent does not support the HTML5 Audio element.</p>
</audio>

jsFiddle here.

注意:我不确定哪个是最好的,因为我从未使用过(还有)。


更新:如另一个答案的评论所述,您正在使用XHTML 1.0 Transitional。你或许可以让<audio>与一些黑客一起工作。


更新2:我记得另一种播放音频的方式。这将在XHTML中工作!这完全符合标准。

你使用这个JavaScript:

var aud = document.createElement("iframe");
aud.setAttribute('src', "http://yoursite.com/youraudio.mp4"); // replace with actual file path
aud.setAttribute('width', '1px');
aud.setAttribute('height', '1px');
aud.setAttribute('scrolling', 'no');
aud.style.border = "0px";
document.body.appendChild(aud);

This is my answer to another question.


更新3:要自定义控件,您可以使用像this这样的东西。


4
投票

HTML5元素肯定是要走的路。在几乎所有浏览器的最新版本中至少有对它的基本支持:

http://caniuse.com/#feat=audio

并且它允许指定浏览器不支持该元素时要执行的操作。例如,您可以通过执行以下操作添加文件的链接:

<audio controls src="intro.mp3">
   <a href="intro.mp3">Introduction to HTML5 (10:12) - MP3 - 3.2MB</a>
</audio>

您可以在以下链接中找到此示例以及有关audio元素的更多信息:

http://hacks.mozilla.org/2012/04/enhanceyourhtml5appwithaudio/

最后,好消息是mozilla的April's dev Derby是关于这个元素的,所以这可能会提供大量如何充分利用这个元素的例子:

http://hacks.mozilla.org/2012/04/april-dev-derby-show-us-what-you-can-do-with-html5-audio/


1
投票

这是一个解决方案,使用有效的xHTML和非侵入式javascript制作一个可访问的音频播放器,这要归功于W3C Web Audio API

该怎么办 :

  1. 如果浏览器能够读取,那么我们显示控件
  2. 如果浏览器无法读取,我们只需呈现该文件的链接

首先,我们检查浏览器是否实现了Web Audio API:

if (typeof Audio === 'undefined') {
    // abort
}

然后我们实例化一个Audio对象:

var player = new Audio('mysong.ogg');

然后我们可以检查浏览器是否能够解码这种类型的文件:

if(!player.canPlayType('audio/ogg')) {
    // abort
}

或者即使它可以播放编解码器:

if(!player.canPlayType('audio/ogg; codecs="vorbis"')) {
    // abort
}

然后我们可以使用player.play()player.pause();

我做了一个小小的JQuery插件,我打电话给nanodio测试这个。

你可以检查它在my demo page上是如何工作的(对不起,但文字用法语:p)

只需点击要播放的链接,然后再次单击即可暂停。如果浏览器可以原生地读取它,它会。如果不能,则应下载该文件。

这只是一个小例子,但你可以改进它以使用页面的任何元素作为控制按钮或使用javascript动态生成...无论你想要什么。


1
投票
<html>

<head>
  <H1>
      Automatically play music files on your website when a page loads
    </H1>
</head>

<body>
  <embed src="YourMusic.mp3" autostart="true" loop="true" width="2" height="0">
  </embed>
</body>

</html>

0
投票

如果您使用的是HTML 5,那么就有<audio>元素。

MDN

audio元素用于在HTML或XHTML文档中嵌入声音内容。音频元素是HTML5的一部分。


更新:

为了在5之前的HTML版本(包括XHTML)中在浏览器中播放音频,您需要使用众多闪存音频播放器中的一个。


0
投票

我发现大多数IE或Chrome都被阻塞了,或者他们需要外部库。我只是想播放MP3,我发现页面http://www.w3schools.com/html/html_sounds.asp非常有帮助。

<audio controls>
  <source src="horse.mp3" type="audio/mpeg">
  <embed height="50" width="100" src="horse.mp3">
</audio>

在我尝试的浏览器中为我工作,但此时我没有一些旧的。

© www.soinside.com 2019 - 2024. All rights reserved.