Wrapping or Embedding and MP3 in a SWF using Flex's mxmlc compiler
Our Flash web-based applications play lots of audio for narration and sound-effects. Some of our customers have firewall rules that block downloading of MP3 and other audio files. So, we need to wrap those MP3 files in SWFs. In the past,开发者_如何学JAVA I've written JSFL scripts that automate the Flash IDE and walk through a complicated, fragile set of steps to embed MP3 files into FLAs and then publish those to SWFs. Now, Flex SDK provides the mxmlc compiler. I've mixed ANT into our workflow, and command-line and automated builds have been a joy. So, I want to make transcoding or wrapping of MP3s part of our build process. I've found Embedding Asset at Compile time in Pure AS3, but this will require that I write a script to generate a wrapper class AS file. Is there a cleaner way to wrap or transcode MP3 files into SWFs? I suppose I'm hoping there is a method for passing the mp3 directly to mxmlc and outputting a swf, but any recommendation better than generating actionscript wrapper classes would be greatly appreciated.
Since you are using MXMLC and Ant already, you should consider adding another bit of code to your Ant build script to build your MP3s into a library SWC. You can then build that SWC into the executable SWF (I've left that simple step out of my example below).
Since all you'll need is Ant, doing it this way is no more difficult than how you're already building your SWF. The only real "gotcha" is that you need to embed your files using an MXMLC/SWC-friendly absolute path (e.g. "/myAssets/myasset.mp3") in your code.
Because it has access to Project metadata, Flash Builder "knows" where the root of your project is, allowing it to use relative embed paths. MXMLC doesn't have any of this info. You therefore need to make sure that the embeds are declared to match the absolute location of how the files are stored in the SWC. If you do this, both Flash Builder and MXMLC/Ant will be able to understand your embeds. That way, everybody's happy.
To help you along, below is an example Ant script for building an asset SWC. Here are the key steps, in a nutshell:
- Build up a String containing the locations of the files to be included, one by one
- Compile those assets into a SWC using MXMLC and a monster-sized set of command-line arguments
The following script will package jpgs, pngs, svgs, ttfs, xml files, properties files and MP3s into a file called "assets.swc". You'll need to include flexTasks.jar (for obvious reasons) and ant-contrib.jar in the appropriate relative locations and set a FLEX_HOME environment variable.
<?xml version="1.0" encoding="utf-8"?>
<project name="My App Builder"
basedir="."
default="buildSWC"
xmlns:antcontrib="antlib:net.sf.antcontrib">
<taskdef resource="flexTasks.tasks" classpath="${basedir}/libs/flexTasks.jar"/>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${basedir}/libs/ant-contrib-1.0b3.jar"/>
<property environment="env"/>
<property name="FLEX_HOME" value="${env.FLEX_HOME}"/>
<property name="ASSETS_FILE" value="assets.swc"/>
<property name="SRC_DIR" value="./src"/>
<!-- Prepare folders for SWC compilation -->
<target name="buildSWC">
<echo message=""/>
<echo message="*****************************************************"/>
<echo message="* ${ASSETS_FILE}"/>
<echo message="*****************************************************"/>
<echo message="...basedir: ${basedir}"/>
<!-- Build a swc from statically-included assets (images, mp3s, xml files, properties files) -->
<fileset id="assets.flex" dir="src" includes="**/*.jpg,**/*.png,**/*.mp3,**/*.css,**/*.svg,**/*.swf,**/*.TTF,**/*.jpeg,**/*.xml,**/*.properties"/>
<pathconvert pathsep=" " property="assets.flex.output" refid="assets.flex" dirsep="/">
<map from="${basedir}/src/" to=""/>
</pathconvert>
<echo message="...Resources being considered..."/>
<var name="filelist" value=""/>
<var name="prefixfilelist" value="-include-file"/>
<for list="${assets.flex.output}" delimiter=" " param="asset">
<sequential>
<echo>Asset: @{asset}</echo>
<propertyregex property="prop"
input="${asset}"
regexp="(.*)${SRC_DIR}/(.*)"
select="\2"
casesensitive="false"
defaultvalue="./src/"/>
<echo>Prop: ${prop}</echo>
<var name="filelist_tmp" value="${filelist}"/>
<var name="filelist" unset="true"/>
<var name="filelist"
value="${filelist_tmp} ${prefixfilelist} ./@{asset} ${prop}@{asset}"/>
<var name="prop" unset="true"/>
</sequential>
</for>
<echo message="-output ${ASSETS_FILE} ${filelist}"/>
<!-- Windows Compile -->
<exec executable="${FLEX_HOME}/bin/compc.exe"
failonerror="true"
osfamily="winnt">
<arg line="-output ./libs/assets.swc ${filelist}"/>
</exec>
<!-- Unix/Linux Compile -->
<exec executable="${FLEX_HOME}/bin/compc"
failonerror="true"
osfamily="unix">
<arg line="-output ./libs/assets.swc ${filelist}"/>
</exec>
</target>
</project>
We use this approach (which I pieced together from bits and pieces I found on the internet -- I'd gladly give credit if I remembered where) to build a large, module-based project and its embedded images and fonts. There is no reason to think that it wouldn't work for audio files.
Good luck,
Taylor
P.S. There might be some leftover/useless lines of code in there. Also, I'm not an Ant expert, so to any "Ant guys" out there: take it easy on me if I broke any best practices ;)
精彩评论