最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can an SVG contain audio using javascript? - Stack Overflow

programmeradmin5浏览0评论

I have an SVG with some basic javascript interactivity, and I would love to be able to put some audio into it as well.

To clarify:

  • This is for an SVG that is by itself, not inlined in HTML
  • I would prefer to avoid having to use a javascript library, but i can if it's necessary.

Any insight on how this could be accomplished?

I have an SVG with some basic javascript interactivity, and I would love to be able to put some audio into it as well.

To clarify:

  • This is for an SVG that is by itself, not inlined in HTML
  • I would prefer to avoid having to use a javascript library, but i can if it's necessary.

Any insight on how this could be accomplished?

Share Improve this question asked Jan 19, 2013 at 7:12 SirCxyrtyxSirCxyrtyx 3873 silver badges11 bronze badges 2
  • Out of curiosity, how do you accomplish JS interactivity without an HTML wrapper? – HellaMad Commented Jan 19, 2013 at 7:15
  • 3 @DC_: Just like with HTML: Using a <script> element and/or onclick() etc. attributes. – Thomas W Commented Jan 19, 2013 at 8:14
Add a comment  | 

2 Answers 2

Reset to default 12

There are SVG <audio> and <video> element tags defined in SVG 1.2 Tiny. Opera supports them but no other UA does as yet.

A solution that will work most places except IE 9/10 would be to use a <foreignObject> element with an html <audio> element as a child.

I don't think there's a good answer if you need IE9/10 support as it doesn't support <foreignObject>

If you require a browser with support for HTML5 audio, then it's possible to just put the audio elements in the svg (use the XHTML namespace for the audio element and its children). Last time I tried it didn't require a foreignObject wrapper. Here's an example showing how to do that (works in chrome, firefox and opera).

It's also possible to create HTML5 audio elements with javascript and use them inside the SVG.

发布评论

评论列表(0)

  1. 暂无评论