⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fssoundconstructortest.java

📁 利用opensource的开源jar实现生成flash文件
💻 JAVA
字号:
package com.flagstone.transform.test;

import java.io.*;

import com.flagstone.transform.*;
import com.flagstone.transform.util.*;

public class FSSoundConstructorTest
{
    private File sourceDir = null;
    private File destDir = null;
    private String extension = null;
    
    public FSSoundConstructorTest()
    {
    }
    /**
     * @testng.configuration beforeTest = "true" alwaysRun = "true" 
     * @testng.parameters value = "srcDir dstDir ext"
     */
    public void configure(String srcDir, String dstDir, String ext)
    {
        sourceDir = new File(srcDir);
        destDir = new File(dstDir);
        extension = ext;
    }
	/**
     * @testng.test dataProvider="files"
	 */
    public void playEventSound(String sndFile)
    {
        try 
        {
            File srcFile = new File(sourceDir, sndFile);
            File destFile = new File(destDir, sndFile.substring(0, sndFile.lastIndexOf('.')) + ".swf");
 
            if (destDir.exists() == false)
                assert destDir.mkdirs() : "Count not create directory: "+destDir;
            
            FSSoundConstructor soundGenerator = new FSSoundConstructor(srcFile.getPath());
            encodeEventSoundToFile(soundGenerator, destFile);
        }
        catch (Exception e)
        {
            assert false;
        }
    }
    /**
     * @testng.test dataProvider="files"
     */
    public void playStreamingSound(String sndFile)
    {
        try 
        {
            File srcFile = new File(sourceDir, sndFile);
            File destFile = new File(destDir, sndFile.substring(0, sndFile.lastIndexOf('.')) + ".swf");
 
            if (destDir.exists() == false)
                assert destDir.mkdirs() : "Count not create directory: "+destDir;
            
            FSSoundConstructor soundGenerator = new FSSoundConstructor(srcFile.getPath());
            encodeStreamingSoundToFile(soundGenerator, destFile);
        }
        catch (Exception e)
        {
            assert false;
        }
    }
    /**
     * @testng.data-provider name="files"
     */
    public Object[][] findFiles()
    {
        FilenameFilter filter = new FilenameFilter() 
        {
            public boolean accept(File directory, String name) 
            {
                String ext = name.substring(name.length()-extension.length()).toLowerCase();
                
                return ext.equals(extension);
            }
        };
        
        Object[][] parameters = new Object[0][0];
        
        if (sourceDir.exists())
        {
            String[] files = sourceDir.list(filter);       
    
            parameters = new Object[files.length][1];
            
            for (int i=0; i<files.length; i++)
                parameters[i] = new Object[] { files[i] };
        }
        
        return parameters;
    }
      
    private void encodeEventSoundToFile(FSSoundConstructor soundGenerator, File file) throws Exception
    {
        FSMovie movie = new FSMovie();
        
        float framesPerSecond = 12.0f;

        movie.setFrameSize(new FSBounds(0, 0, 8000, 4000));
        movie.setFrameRate(framesPerSecond);
    
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));

        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.
         */
        
        FSDefineSound sound = soundGenerator.defineSound(soundId);
        
        movie.add(sound);
        movie.add(new FSStartSound(new FSSound(soundId, FSSound.Start)));

        /* 
         * Add frames to give the sound time to play.
         */
        for (int j=0; j<numberOfFrames; j++)
            movie.add(new FSShowFrame());

        movie.encodeToFile(file.getPath());
    }
    private void encodeStreamingSoundToFile(FSSoundConstructor soundGenerator, File file) throws Exception
    {
        float framesPerSecond = 12.0f;

        FSMovie movie = new FSMovie();

        movie.setFrameSize(new FSBounds(0, 0, 8000, 4000));
        movie.setFrameRate(framesPerSecond);
    
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));

        /*
         * Play the Streaming Sound.
         *
         * 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;

        /* 
         * 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 j=0; j<numberOfBlocks; j++)
        {
            movie.add(soundGenerator.streamBlock(j, samplesPerBlock));
            movie.add(new FSShowFrame());
        }

        movie.encodeToFile(file.getPath());
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -