我使用我在网上找到的以下脚本(AS3)在我的闪存中播放音乐。我不知道动作脚本。
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
function onComplete(evt:Event):void {
//Play loaded sound
sndChannel = soundClip.play();
isPlaying = true;
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}
正如您在上面的脚本中看到的,是播放music_play
和music_stop
停止按钮的实例名称。.mp3 文件在加载时应该播放音乐,但只有当我从 Flash 中查看时才会播放音乐。但是当我在本地或在线 (http://ulfhc.com.au/) 观看时,音乐无法播放。我确定这不是因为 .swf 或 .mp3 文件的位置。
你能帮我解决这个问题吗?
非常感谢。
编辑:
那么新代码会是这样吗?
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
var soundClip:Sound;
function init() {
soundClip = new Sound();
soundClip.load(new URLRequest("music.mp3"));
soundClip.addEventListener(Event.COMPLETE, soundLoaded);
soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
}
init();
function onComplete(evt:Event):void {
sndChannel = soundClip.play();
isPlaying = true;
}
function soundLoaded(e:Event) {
soundClip.play();
}
function soundLoading(e:ProgressEvent) {
// preloader information goes here
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}