2013-09-26 42 views
0

我想在flex中使用聲音。我得到了使用urlrequest的輸出,因爲我使用的聲音文件較少,無需每次加載時都調用它。於是,我就提出,在CSS和使用它,但我得到了一個錯誤:TypeError: Error #1007: Instantiation attempted on a non-constructor.將聲音嵌入到CSS中的flex

下面是我的代碼

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 

<mx:Style> 

draw 
{ 
    url:"Assets/Sound/Active.mp3"; 
} 

</mx:Style> 

<mx:Script> 
<![CDATA[ 

     import flash.media.Sound; 
     import flash.media.SoundTransform; 

    internal var sound:Sound; 
    internal var soundVolume:SoundTransform; 

public function playSound():void 
    { 
     var SoundClass:Class; 

    try 
    { 
      SoundClass = StyleManager.getStyleDeclaration("draw").getStyle("url") as Class; 
      soundVolume = new SoundTransform(1, 0); 
      trace("sound : "+sound); 
      sound = new SoundClass() as Sound; 
      sound.play(0, 0, soundVolume); 
    } 
    catch(E:Error) 
    { 
     trace("E "+E); 
    } 
    } 

]]> 
</mx:Script> 

<mx:Button click="playSound()" label="Discard"/> 
</mx:Application> 

回答

1
draw { 
    url:Embed(source="Assets/Sound/Active.mp3"); 
} 

參考this

+0

'Timofei Davydik'它工作了我,謝謝。 – user1647017

+0

你好@Timofei,很抱歉打擾你很久以後。上面的代碼爲我工作,但CSS顯示錯誤'不能轉碼比特率48000Hz的聲音'。如果我降低了比特率,聲音變得奇怪。請讓我知道如何在CSS中添加更高比特率的聲音。我的聲音文件大小爲21kb-31kb,48000Hz和更高的比特率(近似值)。謝謝 – user1647017

+0

對不起,我不知道... –