小さなサウンドの場合、外部のファイルからサウンドをロードする代わりに埋め込みサウンドを使用するのは最も有効です。

アプリケーションにサウンドファイルを埋め込むと、そのサウンドファイルのサイズだけ SWF ファイルのサイズが増加します。つまり、アプリケーションに大きなサウンドファイルを埋め込むと、SWF ファイルが望ましくない大きなサイズになる可能性があります。

Flex アプリケーションにサウンドアセットを埋め込む場合、次のような方法があります。

  • スクリプトで [Embed] メタデータタグを使用する
  • MXML で @Embed ディレクティブを使用して、埋め込みアセットを Button や SoundEffect などのコンポーネントのプロパティとして割り当てる
  • CSS ファイル内で @Embed ディレクティブを使用する

次のコードは「sounds.mp3」というサウンドファイルをCSS ファイル内で @Embed ディレクティブを使用するサンプルです。

まずは、下記のようなコードをCSSファイルに定義します。

  1. MySound{
  2.   url: Embed(source='assets/sounds.mp3');
  3. }

以下は該当CSSを利用するMXMLソースコードです。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3.   <mx:Script>
  4.     <![CDATA[
  5.  
  6.       // This is the function which plays the sound
  7.       private function playEmbeddedSound():void{
  8.           // create a CSSStyleDeclaration object which uses the
  9.           // StyleManager.getStyleDeclaration to get the class
  10.           // information from the current application css rules
  11.         var soundCSSClassDec:CSSStyleDeclaration = StyleManager.getStyleDeclaration("MySound");
  12.  
  13.         // create the MySoundClass object using the CSSStyleDeclaration.getStyle
  14.         // method to retrieve the embedded source of the mp3 file
  15.         var MySoundClass:Class = (soundCSSClassDec.getStyle("url")) as Class;
  16.         var myEmbeddedSound:Sound = new MySoundClass() as Sound;
  17.         myEmbeddedSound.play();
  18.       }   
  19.  
  20.     ]]>
  21.   </mx:Script>
  22.  
  23.   <!-- add a button to play the sound -->
  24.   <mx:Button click="playEmbeddedSound()" label="Play" />   
  25.  
  26.   <!-- add the stylesheet to the application -->
  27.   <mx:Style source="css/main.css" />
  28. </mx:Application>

そのような使うとCSSの定義より、Themeよってそれぞれのサウンドを切り替えることを出来ます。

メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Sunday, 21st December 2008 by admin

Tags: , , , ,
Posted in Flex | Comments (0) | 2,942 views

Related Posts

Leave a Reply