Created
July 4, 2017 14:27
-
-
Save susisu/f5fe69e226b8226e1b01d3939734fd81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package { | |
| import flash.events.SampleDataEvent; | |
| import flash.media.Sound; | |
| import flash.media.SoundChannel; | |
| import flash.utils.ByteArray; | |
| public class LoopPlayer extends Object{ | |
| private var sound:Sound; | |
| private var loopStart:Number; | |
| private var loopEnd:Number; | |
| private var channel:SoundChannel; | |
| private var player:Sound; | |
| private var bytes:ByteArray; | |
| private var position:Number; | |
| private const SAMPLE_NUM:int=2048; | |
| public function LoopPlayer(sound:Sound,loopStart:Number,loopEnd:Number){ | |
| this.sound=sound; | |
| this.loopStart=loopStart*44.1/SAMPLE_NUM; | |
| this.loopEnd=loopEnd*44.1/SAMPLE_NUM; | |
| player=new Sound(); | |
| bytes=new ByteArray(); | |
| position=0; | |
| } | |
| public function play(startTime:Number=0):void{ | |
| position=startTime*44.1/SAMPLE_NUM; | |
| player.addEventListener(SampleDataEvent.SAMPLE_DATA,onSampleData); | |
| channel=player.play(); | |
| } | |
| public function stop():void{ | |
| channel.stop(); | |
| player.removeEventListener(SampleDataEvent.SAMPLE_DATA,onSampleData); | |
| } | |
| private function onSampleData(e:SampleDataEvent):void{ | |
| bytes.clear(); | |
| sound.extract(bytes,SAMPLE_NUM,position*SAMPLE_NUM); | |
| e.data.writeBytes(bytes); | |
| position++; | |
| if(position>=loopEnd){ | |
| position=loopStart; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment