0

我使用我在网上找到的以下脚本(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_playmusic_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;
}
4

1 回答 1

0

根据本地/网络访问,确保 Flash 发布设置“本地播放安全”设置为“仅访问网络”而不是“仅访问本地文件”

还要确保您的路径与托管部署的环境相匹配。如果您引用“music.mp3”作为 URL,则“music.mp3”必须与您发布的 SWF 位于同一位置。

每预加载 mp3 而不是流式传输,请Event.COMPLETE在调用play()您的声音之前收听:

var soundClip:Sound;

function init() {
    soundClip = new Sound();
    soundClip.load(new URLRequest("<path to sound file>"));
    soundClip.addEventListener(Event.COMPLETE, soundLoaded);
    soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);
}
init();

function soundLoaded(e:Event) {
    soundClip.play();
}

function soundLoading(e:ProgressEvent) {
    // preloader information goes here
}

如果您不需要流式传输 mp3,您也可以将其嵌入到库中:

声音剪辑

声音可以通过以下方式播放:

var soundClip:Sound = new SoundClip(); 
soundClip.play();
于 2012-07-20T19:28:00.630 回答