2012-03-24 63 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同樣的錯誤

相關問題