Flash, movie

B

Boris Gorjan

Does anyone use Flash utilities of Flagstone Software?
http://www.flagstonesoftware.com/

On their page there is an example of how to pack an image inside a flash (and
show it):
http://www.flagstonesoftware.com/code/Example.java
http://www.flagstonesoftware.com/code/ShowImage.java

I wanted to extend that by making a movie out of a series of images. I thought
that all I had to do is create a list of Files and use ShowImage's code inside a
loop. The result is not as expected. A .swf file is generated but when I want to
play it, only the first image is shown. Can anyone tell me what am I doing
wrong? Or what am I not doing that I should?

Here's the code:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.flagstone.transform.FSColorTable;
import com.flagstone.transform.FSDefineObject;
import com.flagstone.transform.FSDefineShape3;
import com.flagstone.transform.FSPlaceObject2;
import com.flagstone.transform.FSSetBackgroundColor;
import com.flagstone.transform.FSShowFrame;
import com.flagstone.transform.FSSolidLine;
import com.flagstone.transform.util.FSImageConstructor;

public class MovieAssembler extends Example {

public static void main(String[] args)
{
try {
new MovieAssembler(args);
}
catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public MovieAssembler(String[] args)
throws Exception
{
super(args);

createMovie();

writeFile("MovieAssembler.swf");
}

/**
*
*/
public static byte[] read(InputStream is) throws IOException {
if(is == null)
return null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
byte[] block = new byte[1024];
int bytesRead = -1;
while( (bytesRead = is.read(block, 0, block.length)) > 0)
baos.write(block, 0, bytesRead);
return baos.toByteArray();
}
finally {
try {
baos.reset();
}
catch(Exception x) {}
try {
baos.close();
}
catch(Exception x) {}
try {
is.close();
}
catch(Exception x) {}
}
}

public void createMovie()
throws Exception
{
movie.setFrameRate(25.0f);
movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));

//A series of jpg files is in this dir
String sourceDirname = "E:/temp/video/frames/";
File sourceDir = new File(sourceDirname);
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return file.getName().endsWith(".jpg");
}
};
File[] imageFiles = sourceDir.listFiles(filter);
if(imageFiles != null) {
for(int i = 0; i < imageFiles.length; i++) {
byte[] content = read(new FileInputStream(imageFiles));
if(content == null)
continue;
FSImageConstructor imageGenerator =
new FSImageConstructor(content);

int imageId = movie.newIdentifier();
int shapeId = movie.newIdentifier();

int xOrigin = imageGenerator.getWidth()/2;
int yOrigin = imageGenerator.getHeight()/2;

// Generate the image defintion
FSDefineObject image = imageGenerator.defineImage(imageId);

/*
* All images must be displayed as a bitmap fill inside a
shape. The
* FSImageConstructor class generates the shape enclosing the
image.
* If no border is required then the line style may be set to null.
*/
FSDefineShape3 shape =
imageGenerator.defineEnclosingShape(shapeId, imageId,
-xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable.black()));

/*
* Add all the objects together to create the movie.
*/
//movie.setFrameRate(25.0f);
movie.setFrameSize(shape.getBounds());
//movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
movie.add(image);
movie.add(shape);
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
movie.add(new FSShowFrame());
}
}
}
}
 
T

Timo Stamm

Boris said:
I wanted to extend that by making a movie out of a series of images. I
thought that all I had to do is create a list of Files and use
ShowImage's code inside a loop. The result is not as expected. A .swf
file is generated but when I want to play it, only the first image is
shown.

This is just a guess (I dont know the API), but I think you are writing
all images on the first frame. You should advance 1 frame after adding
an image.


Timo
 
B

Boris Gorjan

Timo said:
This is just a guess (I dont know the API), but I think you are writing
all images on the first frame. You should advance 1 frame after adding
an image.


Timo

Hm. Might be. But how do I advance one frame?

Meanwhile I "discovered" the actions. Just before adding an image, I say

movie.add(new FSFrameLabel(Integer.toString(i)));

and after the for loop I say

ArrayList actions = new ArrayList();
actions.add(new FSPush("0"));
actions.add(new FSGotoFrame2(false));
FSDoAction frameAction = new FSDoAction(actions);
movie.add(frameAction);

like in the api docs comments/descriptions. This supposedly rewinds the movie to
a first (0th) frame and starts it. But... not in my case.

Any ideas?
 
B

Boris Gorjan

Timo said:
Sorry, no idea. Theres a forum on the site, I would try posting the
question there,


Timo

I nailed it down. ;-)

Forget the "actions", the trick is this:

if(i == 0)
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
else
movie.add(new FSPlaceObject2(shapeId, 1));

It means something like this: initialy you place the first object, for all other
objects you have to _re_place the previous one. And that's what the second
constructor does.

I guess I'll have to get used to Flash quirks. It would certainly help if I knew
anything about ActionScript. <lazymode>Which I don't really want to
do.</lazymode> So, a Flagstone tutorial would come in handy. Anyone from
Flagstone listening?

On to the next issue: sound. Is there an internal Flash format, or does one
simply "link in" a midi or an mp3 (or ...)? How?
 
T

Timo Stamm

Boris said:
I nailed it down. ;-)

Forget the "actions", the trick is this:

if(i == 0)
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
else
movie.add(new FSPlaceObject2(shapeId, 1));

It means something like this: initialy you place the first object, for
all other objects you have to _re_place the previous one. And that's
what the second constructor does.

Thanks for posting the solution.

I guess I'll have to get used to Flash quirks. It would certainly help
if I knew anything about ActionScript.

I do know ActionScript (it's just ECMAScript + some host objects), but
it didn't help at all. The whole API is very specific to the SWF file
format. I guess it might help a lot to read the file format
specification. It is freely available somewhere on macromedia.com.


Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top