001: /*
002: * $Id: MP3SoundCommand.java,v 1.5 2002/07/22 23:16:04 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: */
007:
008: package org.openlaszlo.iv.flash.commands;
009:
010: import org.openlaszlo.iv.flash.parser.*;
011: import org.openlaszlo.iv.flash.api.*;
012: import org.openlaszlo.iv.flash.api.action.*;
013: import org.openlaszlo.iv.flash.api.sound.*;
014: import org.openlaszlo.iv.flash.api.shape.*;
015: import org.openlaszlo.iv.flash.util.*;
016: import org.openlaszlo.iv.flash.cache.*;
017: import org.openlaszlo.iv.flash.url.*;
018:
019: import org.openlaszlo.iv.flash.context.Context;
020: import java.io.*;
021:
022: /**
023: * Class to support the Insert MP3 generator template.<BR>
024: *
025: * @author James Taylor
026: * @author Andrew Wason
027: */
028:
029: public class MP3SoundCommand extends GenericCommand {
030:
031: public MP3SoundCommand() {
032: }
033:
034: public void doCommand(FlashFile file, Context context,
035: Script parent, int frame) throws IVException {
036: // Get object parameters
037:
038: String filename = getParameter(context, "filename", "");
039: boolean cache = getBoolParameter(context, "cache", false);
040: boolean stream = getBoolParameter(context, "stream", false);
041: boolean stopAction = getBoolParameter(context, "stopaction",
042: true);
043: int delay = getIntParameter(context, "delay", 0);
044: String instancename = getParameter(context, "instancename");
045:
046: IVUrl url = IVUrl.newUrl(filename, file);
047:
048: Script script = getInstance().copyScript();
049:
050: FlashBuffer fob = (FlashBuffer) MediaCache.getMedia(url);
051: if (fob == null) {
052: try {
053: fob = new FlashBuffer(url.getInputStream());
054: MediaCache.addMedia(url, fob, fob.getSize(), cache);
055: } catch (IOException e) {
056: throw new IVException(Resource.ERRCMDFILEREAD,
057: new Object[] { filename, getCommandName() }, e);
058: }
059: }
060:
061: Frame stopFrame = null;
062:
063: try {
064: if (stream) {
065: SoundStreamBuilder ssb = SoundStreamBuilder
066: .newSoundStreamBuilder(fob, file.getFrameRate());
067: SoundStreamHead head = ssb.getSoundStreamHead();
068:
069: // Add the SoundStreamHead to the current frame in the parent script
070: parent.getFrameAt(frame).addFlashObject(head);
071:
072: int frameCount = parent.getFrameCount();
073: int f = frame;
074: SoundStreamBlock block;
075:
076: while ((block = ssb.getNextSoundStreamBlock()) != null) {
077: if (f >= frameCount) {
078: parent.newFrame().addFlashObject(block);
079: } else {
080: parent.getFrameAt(f).addFlashObject(block);
081: }
082:
083: f++;
084: }
085:
086: getInstance().def = Shape.newEmptyShape1();
087:
088: stopFrame = parent.getFrameAt(f - 1);
089: } else {
090: MP3Sound sound = MP3Sound.newMP3Sound(fob);
091: // Set the delay if provided
092: if (delay != 0) {
093: sound.setDelaySeek(delay);
094: }
095:
096: SoundInfo soundInfo = SoundInfo.newSoundInfo(0);
097: StartSound startSound = StartSound.newStartSound(sound,
098: soundInfo);
099:
100: Frame newFrame = script.newFrame();
101: newFrame.addFlashObject(startSound);
102:
103: stopFrame = newFrame;
104: }
105: } catch (IOException e) {
106: throw new IVException(Resource.ERRCMDFILEREAD,
107: new Object[] { filename, getCommandName() }, e);
108: }
109:
110: if (instancename != null) {
111: getInstance().name = instancename;
112: }
113:
114: if (stopAction) {
115: stopFrame.addStopAction();
116: }
117: }
118: }
|