音乐无法在.swf本地或在线播放,但在通过Flash®观看时播放

时间:2012-07-20 16:41:48

标签: flash background-music

我使用了以下在线发现的脚本(AS3)来播放我的flash中的音乐。我不知道动作。

//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;
}

1 个答案:

答案 0 :(得分: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,你也可以将它嵌入到库中:

sound-clip

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

var soundClip:Sound = new SoundClip(); 
soundClip.play();