/* * StreamingSounds.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 streaming sounds in a movie. * * For an example of how to play a sound in response to a button being clicked see * the Buttons example. * * To run this example, type the following on a command line: * * java com.flagstone.transform.util.examples.StreamingSounds --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 StreamingSounds extends Example { private FSSoundConstructor soundGenerator = null; public static void main(String[] args) { new StreamingSounds(args); } public StreamingSounds(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 = soundFile.substring(first, last) + ".swf"; try { soundGenerator = new FSSoundConstructor(soundFile); } 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 exceptions are all caught in a single handler to make the code * more readable. */ System.err.println("Cannot load sound file:" + soundFile); } createMovie(); writeFile(fileOut); } void createMovie() { float framesPerSecond = 12.0f; /* * 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); /* * Calculate the number of decoded sound samples played for each frame and * the size, in bytes, of each block compressed sound data. */ int samplesPerBlock = soundGenerator.getSampleRate() / (int) framesPerSecond; int numberOfBlocks = soundGenerator.getSamplesPerChannel() / samplesPerBlock; /* * 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())); /* * An FSSoundStreamHeader2 object defines the attributes of the streaming sound. */ movie.add(soundGenerator.streamHeader(samplesPerBlock)); /* * Add a streaming block for each frame so the sound is played as each frame * is displayed. */ for (int i=0; i