<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
width="400" height="110" layout="vertical"
horizontalAlign="center" verticalAlign="center"
>
<mx:Script>
<![CDATA[
import mx.core.SoundAsset;
import flash.media.*;
[Embed(source="song/0380.mp3")]
[Bindable]
public var Song:Class;
public var mySong:SoundAsset = new Song() as SoundAsset;
public var channel:SoundChannel;
public function playSound():void
{
// 如果已经有音乐了,那先做停止处理
stopSound();
// 播放
channel = mySong.play(0,int.MAX_VALUE);
}
public function stopSound():void
{
if ( channel != null ) channel.stop();
}
]]>
</mx:Script>
<mx:HBox>
<mx:Button label="play" click="playSound();"/>
<mx:Button label="stop" click="stopSound();"/>
</mx:HBox>
</mx:Application>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
width="400" height="110" layout="vertical"
horizontalAlign="center" verticalAlign="center"
>
<mx:Script>
<![CDATA[
import mx.core.SoundAsset;
import flash.media.*;
[Embed(source="song/0380.mp3")]
[Bindable]
public var Song:Class;
public var mySong:SoundAsset = new Song() as SoundAsset;
public var channel:SoundChannel;
public function playSound():void
{
// 如果已经有音乐了,那先做停止处理
stopSound();
// 播放
channel = mySong.play(0,int.MAX_VALUE);
}
public function stopSound():void
{
if ( channel != null ) channel.stop();
}
]]>
</mx:Script>
<mx:HBox>
<mx:Button label="play" click="playSound();"/>
<mx:Button label="stop" click="stopSound();"/>
</mx:HBox>
</mx:Application>
转载于:https://blog.51cto.com/tianhuan/398055