Friday, April 1, 2011

Playing music file from HTML page

This is first time I need this in many years, but finally this day has come: I need to play MP3 file from web page.
Looks like the easiest way to do is by using embed tag, so when I need it is needed to play it, I just call from javascript:
$('#sound_element').html("<embed src='scripts/"+file+".mp3' hidden='true' autostart='true' loop='false'>");
Of course I need to have this div somewhere on the page:
<div id="sound_element"></div>
And it does the magic. I found that it plays flawlessly in Chrome, but for Firefox it asks for Quicktime plugin. But I need it for managed environment, so it is not a big deal.
Finally, I can add some tunes to my blog! ;)

Skip XML declaration in JAXB

By default JAXB outputs XML declaration when you marshal your objects:
For example, this:

JAXBContext context = JAXBContext.newInstance(Command.class, Message.class...);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(command, writer);
will output this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message channel="test"/>


If you don't need to use XML declaration in your JAXB output, it is easy to skip it by setting property "jaxb.fragment" to true. So, this:
JAXBContext context = JAXBContext.newInstance(Command.class, Message.class...);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
marshaller.marshal(command, writer);
will output only this:
<message channel="test"/>