在下面的代码中,我已将我的 mp3 放在此 mxml 文件所在的 src 目录中。因此,当加载 swf 时,我可以看到按钮我无法播放 mp3,这与路径有关
<?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 Sound();
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>