如何管理movieclip按钮控制主时间轴上的声音?

时间:2012-03-24 08:09:50

标签: flash audio button actionscript

我在尝试使用按钮操作音乐时遇到了麻烦

  1. 电影开始时我似乎无法进行music1自动播放和循环播放。

  2. 我想在按下按钮时停止music1并播放music2

      主时间轴中的
    • music1
    • 更改音乐的按钮位于影片剪辑中
  3. 到目前为止,这是我在互联网上找到的代码,但我不确定如何更改它以执行我需要的操作:

    var mySound:Sound;
    var myChannel:SoundChannel;
    var isPlaying:Boolean = false;
    var isPaused:Boolean = false;
    var p:uint = 0;
    var songfile:String;
    var songtitle:String;
    
    song1_btn.addEventListener(MouseEvent.CLICK, playSound);
    song2_btn.addEventListener(MouseEvent.CLICK, playSound);
    song3_btn.addEventListener(MouseEvent.CLICK, playSound);
    stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
    pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
    
    function stopSound(myEvent:MouseEvent):void {
        if (isPlaying) {
            myChannel.stop();
            p = 0;
            isPlaying = false;
            isPaused = false;
        }
    }
    
    function playSound(myEvent:MouseEvent):void {
        switch(myEvent.target.name) {
            case "song1_btn":
                songfile = "bgm1.mp3";
                songtitle = "one";
                break;
            case "song2_btn":
                songfile = "bgm2.mp3";
                songtitle = "two";
                break;
            case "song3_btn":
                songfile = "bgm3.mp3";
                songtitle = "Three";
                break;
        }
        mySound = new Sound;
        mySound.load(new URLRequest(songfile));
        title_txt.text = songtitle;
        if (isPlaying) {
            myChannel.stop();
            myChannel = mySound.play(0);
        } else {
            myChannel = mySound.play(0);
            isPlaying = true;
        }
    }
    
    function pauseSound(myEvent:MouseEvent):void {
        if (isPlaying) {
            p = Math.floor(myChannel.position);
            myChannel.stop();
            isPlaying = false;
            isPaused = true;
        } else if (isPaused) {
            myChannel = mySound.play(p);
            isPlaying = true;
            isPaused = false;
        }
    }
    
    title_txt.text = "This text will be replaced.";
    

    注意:此代码位于主时间轴中。

2 个答案:

答案 0 :(得分:0)

我希望这段代码与你想要的类似,我试着保持简单,以防你想要修改任何东西以更好地满足你的需求:

var mySound:Sound;
var myChannel:SoundChannel;

//song files
var music1:String="music1.mp3";
var music2:String="music2.mp3";

//initially load and loop music1
loopMusic(music1);

//button to change to music2 inside movieclip
myMovieclip.myButton2.addEventListener(MouseEvent.CLICK, button2_handler);

//button to change back to music1 inside movieclip
//initially disabled since music1 is playing
myMovieclip.myButton1.addEventListener(MouseEvent.CLICK, button1_handler);
myMovieclip.myButton1.enabled=false;

function loopMusic(songName:String):void
{
  //stop music currently playing
  if(myChannel)
    myChannel.stop();

  //initialize sound var
  mySound = new Sound();

  //load song
  mySound.load(new URLRequest(songName));

  //set up event listener
  mySound.addEventListener(Event.COMPLETE, songLoaded_handler);

}

function songLoaded_handler(evt:Event):void 
{
//loop loaded song 
    myChannel = mySound.play(0, int.MAX_VALUE);
}

function button1_handler(e:Event):void
{
    loopMusic(music1);

    //toggle buttons
    myMovieclip.myButton2.enabled=true;
    myMovieclip.myButton1.enabled=false;


}

function button2_handler(e:Event):void
{
    loopMusic(music2);

    //toggle buttons
    myMovieclip.myButton1.enabled=true;
    myMovieclip.myButton2.enabled=false;


}

答案 1 :(得分:0)

尝试编辑代码,因为按钮位于2层movieclip下...我的错误..代码但是当我尝试播放电影时它返回错误

var mySound:Sound;
var myChannel:SoundChannel;

//song files
var music1:String="sounds/music1.mp3";
var music2:String="sounds/music2.mp3";

//initially load and loop music1
loopMusic(music1);

//button to change to music2 inside movieclip
mc_university.mc_hymn.myButton2.addEventListener(MouseEvent.CLICK, button2_handler);

//button to change back to music1 inside movieclip
//initially disabled since music1 is playing
mc_university.mc_hymn.myButton1.addEventListener(MouseEvent.CLICK, button1_handler);
mc_university.mc_hymn.myButton1.enabled=false;

function loopMusic(songName:String):void
{
//stop music currently playing
if(myChannel)
myChannel.stop();

//initialize sound var
mySound = new Sound();

//load song
mySound.load(new URLRequest(songName));

//set up event listener
mySound.addEventListener(Event.COMPLETE, songLoaded_handler);

  }

  function songLoaded_handler(evt:Event):void 
  {
  //loop loaded song 
  myChannel = mySound.play(0, int.MAX_VALUE);
  }

  function button1_handler(e:Event):void
  {
  loopMusic(music1);

  //toggle buttons
  mc_university.mc_hymn.myButton2.enabled=true;
  mc_university.mc_hymn.myButton1.enabled=false;


}

    function button2_handler(e:Event):void
{
  loopMusic(music2);

  //toggle buttons
  mc_university.mc_hymn.myButton1.enabled=true;
  mc_university.mc_hymn.myButton2.enabled=false;


}

dunno出了什么问题..即使我把按钮放在主时间轴上或像原始代码一样放在动画片段内,它仍然会出现同样的错误