/* * BasicSounds.java * Examples * * Created by Stuart MacKay on Fri Jun 06 2003. * Copyright (c) 2001-2004 Flagstone Software Ltd. All rights reserved. * * This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING * WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. */ package com.flagstone.transform.util.examples; import com.flagstone.transform.*; import com.flagstone.transform.util.*; import com.flagstone.transform.examples.Example; import java.io.*; /* * This example shows how the FSSoundConstructor class can be used to generate * the objects used to play a sound in a movie. * * To run this example, type the following on a command line: * * java com.flagstone.transform.util.examples.BasicSounds --file sound-file [--resultDir path] * * where * * sound-file is the path to a file containing a WAVE or MP3 format sound. * * resultDir is the directory where the Flash file generated by the example * is written to. * */ public class BasicSounds extends Example { private FSSoundConstructor soundGenerator = null; private FSMovie movie = new FSMovie(); public static void main(String[] args) { new BasicSounds(args); } public BasicSounds(String[] args) { super(args); String soundFile = getOption("file", ""); /* Create the name of the Flash file generated using the name of the * sound file - replaciing the file extension with '.swf'. */ int first = soundFile.lastIndexOf(File.separatorChar); int last = soundFile.lastIndexOf('.'); String fileOut = resultDir + File.separator + soundFile.substring(first, last) + ".swf"; try { /* Create the FSSoundConstructor object used to generate the object * that defines the sound. Initialize it using the name of the * sound file passed on the command line when the exanple was run. */ soundGenerator = new FSSoundConstructor(soundFile); createMovie(); movie.encodeToFile(fileOut); } catch (Exception e) { /* Several exceptions could be thrown to get to this point. The * FSSoundConstructor will throw a FileNotFoundException, IOException * or DataFormatException if the sound is not the correct format or * if an error occurs while loading the file and extracting the * sound. * * The FSMovie will throw a FileNotFoundException or IOException if * there is a problem generating the Flash file. * * The exceptions are all caught in a single handler to make the code * more readable. */ System.err.println("Cannot create file for sound:" + soundFile); } } void createMovie() { float framesPerSecond = 12.0f; int soundId = movie.newIdentifier(); /* * Calculate the time it takes to play the sound and the number of frames * this represents. */ float duration = ((float) soundGenerator.getSamplesPerChannel()) / ((float) soundGenerator.getSampleRate()); int numberOfFrames = (int) (duration * framesPerSecond); /* * Add the sound definition and the FSStartSound object which is used to start * the sound playing. */ //if (soundGenerator.getFormat() == FSSound.PCM) // soundGenerator.compressADPCM(5); FSDefineSound sound = soundGenerator.defineSound(soundId); /* * Add all the objects together to create the movie. */ movie.setFrameSize(new FSBounds(0, 0, 8000, 4000)); movie.setFrameRate(framesPerSecond); movie.add(new FSSetBackgroundColor(FSColorTable.lightblue())); movie.add(sound); /* * Signal the Flash Player to begin playing the sound. */ movie.add(new FSStartSound(new FSSound(soundId, FSSound.Start))); /* * Add enough frames to give the sound time to play. */ for (int i=0; i