GeneralSanguo工作笔记3——使用Sound播放声音

    技术2025-05-20  43

    目标:播放声音

    前提:无

    失败:无法播放wav声音

    正题:

    1、加载声音文件

    2、创建Sound对象

    3、通过Sound对象的play方法创建SoundChannel对象

    4、对SoundChannel对象进行具体操作

    5、代码如下:

    <?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"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <!--[CDATA[ public var request:URLRequest = new URLRequest("sound/lnh2256.mp3"); public var loader:URLLoader = new URLLoader(request); public var channel:SoundChannel; protected function playSound(event:MouseEvent):void { var mySound:Sound = new Sound(request); channel=mySound.play(0,1); } protected function stopSound(event:MouseEvent):void { var mySound:Sound = new Sound(request); channel.stop();; } ]]--> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Button click="playSound(event)" label="播放" id="p"/> <s:Button click="stopSound(event)" label="停止" id="s" x="78" y="0"/> </s:Application>

    最新回复(0)