須先安裝
1.Apollo runtime
2.Apollo SDK 
3.Flex或 Flex SDK 新手最好是有flex啦,這樣才不用以command line編譯檔案
4.Java 2 SDK

首先,如果有flex builder可以先拉出UI,三個button、一個水平Slider,以及一個Text Area
如果沒有就自己開個mxml檔,把它寫出來
我們設定視窗的背景是白色backgroundColor="#FFFFFF"
三個button分別是Select File、play、stop,然後填入click事件fuction名稱

這裡的HBox是Layout,如果要別的可以自己修改,另外Slider命名為volume,範圍是0-100,表示音量上下界。snapInterval是指每次移動多少,這裡就設成1。 dataTipPrecision則是數值精確度,因為snapInterval是1,所以也不需要小數點,就設成0。

background-alpha:"0.5"是讓面板呈現半透明,不過還需要在"專案名-app.xml"(flex才有事先建立,沒有flex的人要自己建)裡找到
<rootContent systemChrome="none" transparent="true" visible="true">player.swf</rootContent>這行,把transparent設為true

<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication     xmlns:mx="http://www.adobe.com/2006/mxml" width="283" backgroundColor="#FFFFFF" height="263">
<mx:Script>
         <![CDATA[ 
//這裡準備填入底下兩段actionscript
 
]] >        
     </mx:Script>
</mx:Script>
<mx:Button label="Select File" click="loadSound()" visible="true"/>
<mx:TextArea id="myTA" visible="true"/>
<mx:HBox>
<mx:Button label="play" click="playSound();" width="51"/>
<mx:Button label="stop" click="stopSound();"/>
</mx:HBox>
<mx:HSlider minimum="0" maximum="100" labels="[0%,100%]" change="changeVolume();" id="volume" snapInterval="1" dataTipPrecision="0"/>

<mx:Style>
Application
{
background-image:"";
background-color:"";
background-alpha:"0.5";
}
</mx:Style>

</mx:ApolloApplication>


這裡是動態播放音樂的function

            import mx.events.FileEvent;
            import mx.containers.Panel;
            import mx.core.SoundAsset;
            import flash.media.*;
            import flash.media.SoundChannel;
            import flash.filesystem.*;
     
            public var file:File;
            public var sc:SoundChannel = new SoundChannel();
            public var mySound:Sound = new Sound();
            public var request:URLRequest = new URLRequest();   

            public function playSound():void
            {                
                stopSound();   //避免同時播放音樂
               request.url=myTA.text;  //從Text Area拿輸出路徑
                try{
                var mySound:Sound = new Sound();
                mySound.load(request);
                sc= mySound.play();
                volume.value=sc.soundTransform.volume*50;  //設定音量
                sc.addEventListener(Event.SOUND_COMPLETE ,next);
                
//音樂播完後啟發事件
                }
                catch(err:IOError){
                  trace(err);
                }
             }

原本apollo有新的檔案系統對話窗,不過不太好用,所以還是使用window內建的對話窗

SoundTransform是先設定一個變數取得Slider的值
再將這個指派給原本的SondTransform,進而改變音量,關於Sound、SoundChannel這一系列的資料可以去看說明檔

            public function next(event:Event):void{
                    trace("complete");      
            }
            public function stopSound():void  {
                if ( sc != null ) sc.stop();
            }
            public function loadSound():void { 
                     file = File.desktopDirectory; 
  //開啟桌面目錄
                     file.addEventListener(Event.SELECT, onFileLoad);
                     // 選擇的事件
                     file.browse([new FileFilter( "mp3", "*.mp3;*.wav")]); 
                     // 選擇聲音檔
            }   
           public function onFileLoad(e:Event):void {
                     myTA.text=file.url;
//顯示路徑
                     playSound();
            } 
           public function changeVolume():void{
                      var transform:SoundTransform = sc.soundTransform;
                       transform.volume = volume.value/100;
                      sc.soundTransform = transform;
            } 


至於其他的功能下次再說吧XD

arrow
arrow
    全站熱搜

    imsearcher 發表在 痞客邦 留言(0) 人氣()