/*
* BasicSound.java
* Cookbook
*
* Copyright (c) 2001-2009 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.
*/
import com.flagstone.transform.*;
import com.flagstone.transform.util.*;
import java.io.IOException;
import java.util.zip.DataFormatException;
/*
* 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 -cp cookbook.jar BasicSound sound-file file-out
*
* where
*
* sound-file is the path to a file containing a WAVE or MP3 format sound.
*
* file-out is the path where the file will be written. If no output file
* is specified then a file named after the example will be written to the
* current directory.
*/
public class BasicSound
{
public static void main(String[] args)
{
try
{
String sound = args[0];
String out = args.length == 1 ? "BasicSound.swf" : args[1];
BasicSound example = new BasicSound();
FSMovie movie = new FSMovie();
example.createMovie(movie, sound);
movie.encodeToFile(out);
}
catch (Exception e)
{
e.printStackTrace();
}
}
void createMovie(FSMovie movie, String soundFile) throws DataFormatException, IOException
{
float framesPerSecond = 12.0f;
int soundId = movie.newIdentifier();
FSSoundConstructor soundGenerator = new FSSoundConstructor(soundFile);
/*
* 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.
*/
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