2012-02-10 58 views
1

製作鋼琴/鍵盤的應用,並試圖找出對此進行設置的最佳方式,並會喜歡上有一個目錄中的任何建議AS3嵌入動態的音頻文件(S)/使用的變量中嵌入

我計劃我的資產文件夾中的每個'樂器'的聲音(piano1/C4.mp3,piano1/D4.mpg,...)

有沒有辦法導入/嵌入所有這些作爲數組?還有一種方法,我可以將一個變量傳遞給這個類來指示我想從哪個文件夾導入這些文件?

東西,將是理想的

var type = "piano2"; //passed from class being called by 
foreach(notes as note){ 
    [Embed(source = 'assets/sounds/'+type +'/'+note+'.mp3')] private const C3:Class; 
    public var c3:Sfx = new Sfx(C3); 
} 

或者,它會是一個更好的主意,有一類爲每個翻出所有的筆記,這些「類型」的?

回答

2

你不能這樣做,使用[Embed()]。嵌入是編譯時間屬性而不是運行時屬性。另一方面,您可以使用將您的mp3資源打包爲可部署(無論是swf還是空中應用程序),並使用Sound.load()

喜歡的東西:

public initialize():void { 
    var type = "piano2"; //passed from class being called by 
    foreach(notes as note){ 
     var url = 'assets/sounds/'+type +'/'+note+'.mp3'; 
     var req:URLRequest = new URLRequest(url); 
     var sound:Sound = new Sound(); 
     sound.addEventListener(Event.COMPLETE, noteLoaded); 
     sound.load(req); 
    } 
} 

private noteLoaded(e:Event):void { 
    // do something with the loaded sound 
} 
0

我自己做了一個腳本來導入圖形資源更快,你可以爲你的聲音做同樣的。 導入.fla中的每個歌曲,名稱與sounds.fla相似。然後,您可以通過創建文件夾來按需要訂購它們。一旦完成,我有一個jsfl腳本來解析我選擇的所有內容,然後創建一個空的類來導入,然後打印出這樣的內容。

//  this is the name   this is the class referencing what i exported in flash 
private ID_PIANO_SOUND_1:Sound = getClassByName(PIANO_SOUND_1); 
private ID_PIANO_SOUND_2:Sound = getClassByName(PIANO_SOUND_2); 
private ID_PIANO_SOUND_3:Sound = getClassByName(PIANO_SOUND_3); 
private ID_GUITAR_SOUND_1:Sound = getClassByName(GUITAR_SOUND_1); 

所以我可以粘貼,並將其推入閃存中的靜態類。您的sounds.fla需要編譯爲swc並嵌入到您的項目中。

祝你好運!