我构建一个flex应用程序来播放声音。我使用FileReference来浏览mp3文件。如何使用SoundReference对象使用Sound Class的播放方法播放该文件。或者我可以将该FileReference对象中的数据加载到byteArray中。可以使用Sound Class播放byteArray吗?谢谢
答案 0 :(得分: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文件并将org文件夹文件复制到我的项目文件夹(作为子文件夹),然后在.as文件和FLA中使用下面的代码那是舞台。
您还需要创建三个精灵或MovieCips以用作按钮。
* 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 :(得分:0)
应该可以。首先从FileReference加载mp3,然后实例化new Sound()
,然后调用loadCompressedDataFromByteArray(loadedBytes,loadedBytes.length)
然后播放它。如果没有FileReference完成,请不要忘记将有问题的ByteArray倒回到起始位置,因为此函数使用当前位置作为文件的开头。
更新:如果您使用的是Flash Player 10,则此方法不可用。为此,请尝试从URLRequest
创建FileReference.name
,然后通过此伪造请求加载声音。
var ur:URLRequest=new URLRequest(fileRef.name);
var snd:Sound=new Sound(ur);
snd.addEventListener(Event.COMPLETE,sndLoaded);
然后您处理已加载的声音。