开发者

Flex play mp3 sound

Inn the below code i have placed my mp3 in the src directory where this mxml file resides.So when the swf is loaded i can see the button i am not able to get the mp3 playing is that something to do do with the path

  <?xml version="1.0" encoding="utf-8"?>
   <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
    <![CDATA[
        import flash.display.Sprite;
        import flash.display.InteractiveObject;
        import flash.display.Sprite;
        import flash.media.*;
        import flash.net.*;
        import mx.controls.Alert;
        import mx.controls.Button;
        import flash.events.Event;
        import flash.media.Sound;
        import flash.net.URLRequest;



        private function clickhandler(event:Event):void
        {
            var s:Sound = new So开发者_如何学编程und();
            s.addEventListener(Event.COMPLETE, onSoundLoaded);
            var req:URLRequest = new URLRequest("sound.mp3");   
            s.load(req);

        }

        private function onSoundLoaded(event:Event):void
        {
            var localSound:Sound = event.target as Sound;
            localSound.play();
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Button id="play" label="PLAY" click="clickhandler(event)"  />

</s:Application>


In the line:

var req:URLRequest = new URLRequest("sound.mp3");

you need to specify the full URL to the sound.mp3. Something like

var req:URLRequest = new URLRequest("http://server.name.com/path/to/sound.mp3");

If you don't want to compile the path into your application, you can use the Application.url property, which gives you the URL from which your application was loaded. If you know where your mp3 lives relative to that, you can modify the path appropriately and load from there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜