2012-03-24 35 views
1

我无法尝试使用按钮如何管理影片剪辑按钮控制主时间轴上的声音?

  1. 操控音乐,我似乎无法使music1自动播放和循环播放影片开始时。

  2. 我想停止music1和播放music2当我按下一个按钮:在主时间轴

    • music1
    • 按钮来改变音乐是影片剪辑

内到目前为止这里是我在互联网上找到的代码,但我不知道如何改变它来做我所需要的:

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."; 

说明:此代码位于主时间轴上。

+0

你说的那些'music1'和'music2'是什么?我没有在您发布的代码中看到他们?他们是否是歌曲文件,歌曲名称,类型声音的变量......?另外,主要时间线上'music1'的含义是什么? – danii 2012-03-25 11:15:23

+0

哎呀对不起,因为缺乏细节..我在互联网上发现这个代码,并认为它可能类似于我想要做的 我的意思是说,我希望它能像这样工作 1. music 1 will自动播放电影和循环直到我关闭窗口 2.然后我有一个动画片内的按钮,将目前的音乐(音乐1)变成音乐2 基本上,即时制作一个交互式的投资组合,其中有背景音乐播放。然后我有一个页面,将播放赞美诗,所以我需要一个按钮来改变背景音乐的赞美诗,反之亦然 – 2012-03-26 07:21:12

+0

为1)创建一个movieclip并将声音文件放在框架上, – user959631 2012-03-26 20:33:23

回答

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; 


} 
+0

感谢您的回复,我试了代码,它返回了一个错误 TypeError:错误#1009:无法访问空对象引用的属性或方法。 \t at trial2_fla :: MainTimeline/loopMusic() \t at trial2_fla :: MainTimeline/frame1() – 2012-03-26 17:55:11

+0

在代码中可能缺少某些东西?我不知道如何解决它..我欣赏它,如果你可以再次帮助我,谢谢 – 2012-03-26 18:33:40

+0

我的不好,当然你会得到错误1009,我只是意识到我没有把我初始化mySound var的线...我纠正了代码并进行测试,现在它可以正常工作。还有另一行缺失,请在尝试之前更新所有代码。对不起草稿复制和粘贴,希望这有助于解决您的问题! – danii 2012-03-26 20:30:24

0

尝试编辑代码,因为按钮正在2层影片剪辑..我的错误..继承人的代码,但它当我试图播放电影

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; 


} 

不知道什么地方出了错返回一个错误。即使我把按钮放在主时间轴或在像原始代码那样的动画片段内,它仍然给出了th e同样的错误

相关问题