Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 10:46, 30 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Adding sound to SVG animation with XML parsing in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Tested on Nokia E52, Nokia N97 mini, Nokia 5800 XpressMusic
Created: User:Kbwiki (23 Nov 2010)
Last edited: hamishwillee (30 Jul 2013)


The Scalable 2D Vector Graphics API (JSR-226) conforms to SVG Tiny 1.1 and does not provide support for multimedia. The multimedia functionality as described by SVG Tiny 1.2 is supported by the Scalable 2D Vector Graphics API 2.0 (JSR-287) which is currently not supported by Nokia devices.

The multimedia limitation on JSR-226 can be circumvented by using XML parsing which is provided by the Web Services API (JSR-172).

Below is an example application which utilises the described XML parsing solution for playing audio: reading the <audio> tag from the XML file (here: the SVG content file content3.svg) and its attributes from an SVG file are enabled by using XML parsing. Once the attributes have been retrieved, the Mobile Media API (JSR-135) is used to implement the audio playback functionality for the application.

The source file below includes key code snippets from the application. The full code together with a compiled Java MIDlet can be downloaded from the packaged ZIP file linked at the bottom of this page.

The original SVG example (without the XML parsing solution) which has been used for making this XML parsing example can be found in Nokia Developer Java Developer's Library: Java Developer's Library 3.6 > Source codes for examples > Scalable 2D Vector Graphics - HelloWorldMidlet, LoadStaticMidlet, and SvgAnimatorMidlet

Source file

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.m2g.*;
import javax.xml.parsers.*;

public class SvgAnimatorMidlet extends MIDlet implements CommandListener {
 public SvgAnimatorMidlet() {
   SVGImage svgImage = null;
    try {
         InputStream svgStream =getClass().getResourceAsStream("content3.svg")
         svgImage = (SVGImage)( SVGImage.createImage( svgStream, null ) );
    catch ( Exception e ){
   svgAnimator = SVGAnimator.createAnimator( svgImage );
   svgAnimator.setSVGEventListener( new MySvgEventListener() );
   Canvas c = (Canvas)(svgAnimator.getTargetComponent());
   Display.getDisplay(this).setCurrent( c );

 // Implements the XML parser for playing audio 

 public void parseData() {
    try {
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         InputStream is = getClass().getResourceAsStream("content3.svg");
         saxParser.parse(is,new XmlHandler(this));
    catch(Exception ex) {
         System.out.println("parsing exception");
 public void startApp() {;      

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.util.*;

/* Implements the XML handler for processing audio tag and 
its attributes from the SVG file, sets and starts the audio player by
using the retrieved attributes */

public class XmlHandler extends DefaultHandler{
 private SvgAnimatorMidlet midlet;
 private Player p;  
 private static final String audio = "audio";
 private static final String xlink = "xlink:href";
 private static final String type = "type";
 String attribute1="";
 String attribute2="";
 public XmlHandler (SvgAnimatorMidlet midlet){
   this.midlet = midlet;
 public void startDocument() throws SAXException {
  // No implementation

 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException{
     attribute1 = attributes.getValue(xlink);
     attribute2 = attributes.getValue(type);
 public void play()
    try {
         InputStream is = getClass().getResourceAsStream(attribute1);
         p = Manager.createPlayer(is, attribute2);
   }catch (IOException ioe) {
   }catch (MediaException me) { 
 public void endElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException{
 public void endDocument() throws SAXException{
  //No implementation 


<?xml version="1.0" standalone="no"?>
<svg width="176" height="178" stroke="none" baseProfile="tiny" 
xmlns:xlink="" timelineBegin="onLoad">

<audio xlink:href='background_music.wav' volume='7' type='audio/X-wav'
 begin='mybutton.mouseover' end='mybutton.mouseout' repeatCount='indefinite'/>

Sample application

See also:

JSR 172: XML Parsing Example

This page was last modified on 30 July 2013, at 10:46.
88 page views in the last 30 days.