Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an SVG contain audio using javascript?

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?

like image 894
SirCxyrtyx Avatar asked Jan 19 '13 07:01

SirCxyrtyx


People also ask

Can SVG contain audio?

The default event-base element for all SVG media elements is the element itself. The following elements are media elements: 'audio' 'video'

What is SVG in js?

Scalable Vector Graphics (SVG) are an XML-based markup language for describing two-dimensional based vector graphics.

What is an embedded SVG?

Embedded content is content that imports into the document from another resource. SVG <image> and <foreignObject> elements are used for SVG embedded content support.


2 Answers

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.

like image 82
Erik Dahlström Avatar answered Oct 11 '22 21:10

Erik Dahlström


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>

like image 22
Robert Longson Avatar answered Oct 11 '22 19:10

Robert Longson