2013-08-23 159 views
0

我構建了一個彈性應用程序來播放聲音。我使用FileReference來瀏覽mp3文件。如何使用Sound Class的播放方法使用該FileReference對象播放該文件。或者我可以將該FileReference對象的數據加載到byteArray中。可以使用Sound Class播放該byteArray嗎?謝謝如何使用FileReference播放聲音?

回答

0

應該有可能。首先你從FileReference中加載一個mp3,然後你實例化new Sound(),然後調用loadCompressedDataFromByteArray(loadedBytes,loadedBytes.length)然後播放它。如果不是由FileReference完成,不要忘記將相關的ByteArray倒回到起始位置,因爲此函數使用當前位置作爲文件的起始位置。

The manual.

更新:如果您在使用Flash Player 10,此方法不可用。爲此,請嘗試從FileReference.name中創建一個URLRequest,然後通過此僞造的請求加載聲音。

var ur:URLRequest=new URLRequest(fileRef.name); 
var snd:Sound=new Sound(ur); 
snd.addEventListener(Event.COMPLETE,sndLoaded); 

然後處理已加載的聲音,準備就緒。

+0

我找不到這個方法,當我嘗試調用Sound Class方法時。我做錯了什麼? –

+0

我正在使用sdk 3.6。這是我無法使用Sound Class的API的原因嗎? –

+0

嗯,它說它是在FP11和AIR 3中引入的。 – Vesper

1

如果您使用FileReference將mp3文件加載到Sound對象中,那麼它將無法正確播放,因爲數據是壓縮的(音頻將是噪音)。

反正有代碼,也解決了你的問題的另庫...: http://flexiblefactory.co.uk/flexible/?p=46

裏面的文章,你還會發現一個鏈接到這個zip文件:
http://www.flexiblefactory.co.uk/flexible/wp-content/uploads/mp3filereferenceloaderlib.zip

我不不使用Flex,而是在Flash CS中進行設置我將進入zip文件並將組織文件夾文件複製到我的項目文件夾(作爲子文件夾),然後在.as文件中包含以下代碼以及作爲舞臺的FLA。

您還需要創建三個精靈或MovieCips用作按鈕。

  • 文件打開(實例名稱:btn_open)
  • 播放(實例名稱:btn_play)
  • 停止(實例名稱:btn_stop)

* AS3代碼*

package 
{ 

import flash.display.MovieClip; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.events.IOErrorEvent; 
import flash.net.FileFilter; 
import flash.net.FileReference; 
import flash.net.URLLoader; 
import flash.net.URLRequest; 
import flash.net.URLLoaderDataFormat; 
import flash.utils.ByteArray; 
import flash.media.Sound; 
import flash.media.SoundChannel; 

import org.audiofx.mp3.MP3FileReferenceLoader; 
import org.audiofx.mp3.MP3SoundEvent; 


public class MP3_fileRef_test extends MovieClip 

{ 
    private var loader:MP3FileReferenceLoader; 
    private var fileReference:FileReference; 
    public var _loadedsound:Sound = new Sound; 
    public var channel_obj:SoundChannel = new SoundChannel(); 
    public var mp3_isPlaying:Boolean; 


    public function MP3_fileRef_test() 
    { 
     mp3_isPlaying = false; 

     btn_open.buttonMode = true; 
     btn_open.addEventListener(MouseEvent.CLICK, mp3Open_handler); 

     btn_play.buttonMode = true; 
     btn_play.addEventListener(MouseEvent.CLICK, audio_play); 

     btn_stop.buttonMode = true; 
     btn_stop.addEventListener(MouseEvent.CLICK, audio_stop); 

     loader = new MP3FileReferenceLoader(); 
     loader.addEventListener(MP3SoundEvent.COMPLETE, mp3Loader_Complete); 

     fileReference=new FileReference(); 
     fileReference.addEventListener(Event.SELECT,fileReferenceSelectHandler); 
    } 

    //OPEN BROWSE WINDOW & TRIGGER EVENT FUNCTION fileReferenceSelectHandler 
    private function mp3Open_handler (ev:MouseEvent):void 
    { 
     fileReference.browse([new FileFilter("mp3 files","*.mp3")]); 
    } 

    private function fileReferenceSelectHandler(e:Event):void 
    { 
     loader.getSound(fileReference); 
    } 

    private function mp3Loader_Complete (loader_mp3Data:MP3SoundEvent):void 
    { 
     //CHECK IF THERE'S A PREVIOUSLY OPENED FILE PLAYING 
     //OTHERWISE WILL BE MULTIPLE SOUNDS PLAYING OVER EACH OTHER 
     if (mp3_isPlaying == true) 
     { 
      channel_obj.stop(); 
      mp3_isPlaying = false; 
     } 

     _loadedsound = new Sound; 
     _loadedsound.addEventListener(Event.COMPLETE, soundLoad_Complete); 

     //PUT (MP3SoundEvent) LOADER DATA INTO NEW _loadedsound SOUND OBJECT 
     _loadedsound = loader_mp3Data.sound; 

     //UNLOCK BELOW FOR AUTO-START WITHOUT PRESSING PLAY BUTTON 
     //channel_obj = _loadedsound.play(); //auto 
     //mp3_isPlaying = true; //auto 
    } 

    //WHEN SOUND IS LOADED AND READY FOR USE 
    function soundLoad_Complete(e:Event):void 
    { 
     //Do whatever else is needed on successful load 
     //eg: trace or update status text, make an MC visible etc 
    } 

    //PLAY AUDIO ON CLICK 
    function audio_play (e:MouseEvent):void 
    { 
     if (mp3_isPlaying == false) 
     { 
      channel_obj = _loadedsound.play(); 
      mp3_isPlaying = true; 
     } 
    } 

    //STOP AUDIO ON CLICK 
    function audio_stop (e:MouseEvent):void 
    { 
     if (mp3_isPlaying == true) 
     { 
      channel_obj.stop(); 
      mp3_isPlaying = false; 
     } 
    } 


}//END CLASS 

}//END PACKAGE 

希望它有幫助。和平我要離開這裏......

+1

鏈接已死,但我在以下鏈接中找到了源代碼:https://code.google.com/p/in-spirit/source/browse/trunk/projects/FluidSolver3D/src/?r=91# SRC%2Forg%2Faudiofx%2Fmp3 –