【AS3开源库】任意导入MP3文件进行播放 MP3FileReferenceLoaderLib
通常官方提供的Mp3播放API在SWF中 只能播放指定路径下MP3文件。并没有提供类似于加载任意位置图片的特定API。受于一些局限,在网上寻得一开源库MP3FileReferenceLoaderLib,能够通过FileReference来导入任意位置的mp3文件并进行播放。
例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | package mp3Test { import flash.events.Event; import flash.events.MouseEvent; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.FileFilter; import flash.net.FileReference; import org.audiofx.mp3.MP3FileReferenceLoader; import org.audiofx.mp3.MP3SoundEvent; /** *导入外部MP3 * */ public class Mp3Test extends MainV { private var mMp3Loader:MP3FileReferenceLoader; private var mFileRef:FileReference; private var mFileFilter:FileFilter; private var mSound:Sound; private var mSoundChannel:SoundChannel; private var mStartTime: Number = 0 ; private var mIsPlaying: Boolean ; public function Mp3Test() { mMp3Loader = new MP3FileReferenceLoader( ); mFileRef = new FileReference( ); mFileFilter = new FileFilter( '.mp3' , '*.mp3' ); mFileRef.addEventListener( Event.SELECT,onSoundSelectedHandler ); mMp3Loader.addEventListener( MP3SoundEvent.COMPLETE,onSoundLoadedHandler ); this .playBtn.addEventListener( MouseEvent.CLICK,onPlaySoundHandler ); this .stopBtn.addEventListener( MouseEvent.CLICK,onStopSoundHandler ); this .btnScan.addEventListener( MouseEvent.CLICK,onScanSoundHandler ); } private function onPlaySoundHandler( e:MouseEvent ): void { if ( mSound != null && !mIsPlaying ) { mSoundChannel = mSound.play( mStartTime ); mIsPlaying = true ; } } private function onStopSoundHandler( e:MouseEvent ): void { if ( mSoundChannel != null && mIsPlaying ) { mStartTime = mSoundChannel.position; mSoundChannel.stop( ); mIsPlaying = false ; } } private function onScanSoundHandler( e:MouseEvent ): void { mFileRef.browse( [ mFileFilter ] ); } private function onSoundSelectedHandler( e:Event ): void { mMp3Loader.getSound( mFileRef ); } private function onSoundLoadedHandler( e:MP3SoundEvent ): void { if ( mSoundChannel != null ) mSoundChannel.stop( ); mSound = e.sound; mSoundChannel = mSound.play( ); this .infoT.text = mFileRef.name; mIsPlaying = true ; } } } |