小さなサウンドの場合、外部のファイルからサウンドをロードする代わりに埋め込みサウンドを使用するのは最も有効です。
アプリケーションにサウンドファイルを埋め込むと、そのサウンドファイルのサイズだけ SWF ファイルのサイズが増加します。つまり、アプリケーションに大きなサウンドファイルを埋め込むと、SWF ファイルが望ましくない大きなサイズになる可能性があります。
Flex アプリケーションにサウンドアセットを埋め込む場合、次のような方法があります。
- スクリプトで [Embed] メタデータタグを使用する
- MXML で @Embed ディレクティブを使用して、埋め込みアセットを Button や SoundEffect などのコンポーネントのプロパティとして割り当てる
- CSS ファイル内で @Embed ディレクティブを使用する
次のコードは「sounds.mp3」というサウンドファイルをCSS ファイル内で @Embed ディレクティブを使用するサンプルです。
まずは、下記のようなコードをCSSファイルに定義します。
- MySound{
- url: Embed(source='assets/sounds.mp3');
- }
以下は該当CSSを利用するMXMLソースコードです。
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
- <mx:Script>
- <![CDATA[
- // This is the function which plays the sound
- private function playEmbeddedSound():void{
- // create a CSSStyleDeclaration object which uses the
- // StyleManager.getStyleDeclaration to get the class
- // information from the current application css rules
- var soundCSSClassDec:CSSStyleDeclaration = StyleManager.getStyleDeclaration("MySound");
- // create the MySoundClass object using the CSSStyleDeclaration.getStyle
- // method to retrieve the embedded source of the mp3 file
- var MySoundClass:Class = (soundCSSClassDec.getStyle("url")) as Class;
- var myEmbeddedSound:Sound = new MySoundClass() as Sound;
- myEmbeddedSound.play();
- }
- ]]>
- </mx:Script>
- <!-- add a button to play the sound -->
- <mx:Button click="playEmbeddedSound()" label="Play" />
- <!-- add the stylesheet to the application -->
- <mx:Style source="css/main.css" />
- </mx:Application>
そのような使うとCSSの定義より、Themeよってそれぞれのサウンドを切り替えることを出来ます。
メインコンテンツEND ■
Posted on Sunday, 21st December 2008 by admin
Tags: CSS, Flex, MP3, SWF, サウンド
Posted in Flex | Comments (0) | 2,942 views
