声音自动播放 - 闪光

时间:2010-12-15 21:12:57

标签: flash actionscript-3

在我的flash项目的一个框架上有6个按钮。当您单击按钮时,将播放另一首歌曲。简单。问题是,列表中的第一首歌是播放第二个项目加载而不是等待某人点击按钮!

import fl.events.SliderEvent;
var snd:Sound;
var channel:SoundChannel;
var trans:SoundTransform;
var currSong:String;
var currVol:Number = .5;
var currPan:Number = 0;


var songList:Array=new Array("Drifting.mp3","Push.mp3",
                             "Stupid.mp3","Train Wreck.mp3","World on Fire.mp3","Fallen.mp3");


panSlide.visible=false;
volSlide.visible=false;


song1.addEventListener(MouseEvent.CLICK, chooseSong);
song2.addEventListener(MouseEvent.CLICK, chooseSong);
song3.addEventListener(MouseEvent.CLICK, chooseSong);
song4.addEventListener(MouseEvent.CLICK, chooseSong);
song5.addEventListener(MouseEvent.CLICK, chooseSong);
song6.addEventListener(MouseEvent.CLICK, chooseSong);
panSlide.addEventListener(SliderEvent.CHANGE, panChange);
volSlide.addEventListener(SliderEvent.CHANGE, volumeChange);



function chooseSong(e:MouseEvent):void {
    switch (e.currentTarget.name) {
        case "song1":
            currSong = "../MP3s/"+songList[0] as String;
            break;

        case "song2":
            currSong = "../MP3s/"+songList[1] as String;
            break;

        case "song3":
            currSong = "../MP3s/"+songList[2] as String;
            break;

        case "song4":
            currSong = "../MP3s/"+songList[3] as String;
            break;

        case "song5":
            currSong = "../MP3s/"+songList[4] as String;
            break;

        case "song6":
            currSong = "../MP3s/"+songList[5] as String;
            break;
    }
    if (snd != null) {
        channel.stop();
    }
    snd = new Sound();
    snd.load(new URLRequest(currSong));

    channel = new SoundChannel  ;
    trans = new SoundTransform(currVol,currPan);
    channel = snd.play();
    channel.soundTransform = trans;
    panSlide.visible = true;
    volSlide.visible = true;
}


function volumeChange(e:SliderEvent):void {
    currVol = e.target.value;
    trans.volume = currVol;
    channel.soundTransform = trans;
}



function panChange(e:SliderEvent):void {
    currPan = e.target.value;
    trans.pan = e.target.value;
    channel.soundTransform = trans;
}

2 个答案:

答案 0 :(得分:1)

这只是一个黑暗中的镜头,但你不会偶然将歌曲添加到舞台的某个地方?这可能会也可能不会导致歌曲在电影加载时自动播放。

检查您的控件,可能是一个简单的错误点击问题。

答案 1 :(得分:1)

你确定你没有意外地在时间轴上添加声音吗?如果不是,您应该在方法chooseSong中添加一条跟踪,并检查它是否在加载项目时被触发。

我在舞台上创建了一个带有mc(song1)的新文件和这个脚本。这对你有用吗?

song1.addEventListener(MouseEvent.CLICK, chooseSong);

function chooseSong(e:MouseEvent):void {
    trace (e.currentTarget.name)
}