Manipulating MP3 speed and direction
Here is one of those examples that I did just for the hell of it. I often wondered how hard it would be to play an MP3 in reverse. It turns out that it is really easy with Flash Player 10. Now my implementation of reverse is pretty weak and I’m sure guys like Andre Michelle could make something that sounded perfect. Click the button below to try it out. You can reverse direction and speed it up.
[/kml_flashembed]
You surely heard the jerkiness and little glitches and that could surely be corrected with more work. All of this is possible thanks to the new Sound.extract() method. This allows you to grab the bytes from a loaded sound and then manipulate them before passing them to the sound card. You tell the method how many sound samples you want to extract and what position to start from in the file. It is this position property that allows you to move in any direction in the file. Subtracting a number makes it go in reverse and adding to it moves the music forward.
Now one thing that screwed me up at first is the fact that the extract method deals with samples, not bytes. A sample is 8 total bytes of audio data with 4 bytes being for each channel. Another thing that is tricky is getting the total number of samples in a file. I used the method of multiplying 44100 by the number of seconds in the file. Obviously it will only work on files set at that sample rate and it isn’t foolproof. But this method could be useful to allow people to scrub back and forth through an audio file.
The code is shown below and you can also download the FLA. I created in Flash but you could easily dump this into Flash Builder as well. I’d be interested to hear if someone gets it sounding totally smooth. The real trick would be extract a chunk of samples and then reverse the bytes of the samples. I tried that but couldn’t figure it out.
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 | var ex:Sound; var ns:Sound; var position:int; var max:int; var speed:int = 4096; c.b1.addEventListener(MouseEvent.CLICK, function(){speed=-8192;}); c.b2.addEventListener(MouseEvent.CLICK, function(){speed=-4096;}); c.b3.addEventListener(MouseEvent.CLICK, function(){speed=4096;}); c.b4.addEventListener(MouseEvent.CLICK, function(){speed=8192;}); lclip.visible = false; c.visible = false; loadmusic.addEventListener(MouseEvent.CLICK, init); function init(e:Event):void { position = 0; loadmusic.visible = false; lclip.visible = true; ex = new Sound(new URLRequest("col.mp3")); ex.addEventListener(Event.COMPLETE, onComplete); ns = new Sound(); ns.addEventListener(SampleDataEvent.SAMPLE_DATA, onData); } function onComplete(e:Event):void { lclip.visible = false; c.visible = true; max = 44100 * (ex.length/1000) - 4096; position = max; ns.play(); } function onData(e:SampleDataEvent):void { var bytes:ByteArray = new ByteArray(); ex.extract(bytes, 4096, position); e.data.writeBytes(bytes); position += speed; if(position < 0) position = max; else if(position > max) position = 0; } |
New update available for Flash CS4!
| I am very happy to announce that today we shipped a free update to Flash CS4 which addresses most of the issues that people have been having with it. Many of you gave some very honest feedback in this post and I want you to know that it was instrumental in getting this update out. The Flash team has been working around the clock to fix these issues so I want to give a big shout out to them for their tireless work. Product manager Richard Galvan has all the details on the bugs fixed in this update. Go and get the update and please let me know how it works for you. |
Lee
Custom Flex preloader video tutorial
| This week I set out to try and create five new tutorials in five days. Well I have just uploaded the fifth tutorial that shows you how to create a completely customized Flex preloader using Flash CS4. One thing that really bugs me about most Flex applications is the standard aqua loading screen. This video shows how simple it is to customize that experience so there is no longer any excuse for aqua . |
Lee
New snippets panel for Flash CS4
![]() |
Many of you will remember my snippets panel that I released a while back for Flash CS3. Well I have rebuilt it from scratch for Flash CS4. First of all I wanted to build it in Flex so I could take advantage of all the nice layout stuff. I also skinned the panel so it looks right at home with the other panels. Lastly I simplified the panel quite a bit to avoid bugs. All you do is click Edit XML and you will be able to edit it inside of Flash. When you are done just click Refresh to update the panel. The XML structure has also been changed to use CDATA sections for the code which makes it easier to create new snippets. You should be able to figure it out by looking at the XML file. Now it is important to note that the old panel will work in Flash CS4 and this version will work in Flash CS3. |
To install, download the ZIP file and extract the contents to your WindowSWF directory. You can find that directory at the following locations:
Windows: \Documents and Settings\[USER]\Local Settings\Application Data\Adobe\Flash CS4\language\Configuration\WindowSWF
On Windows the path may be slightly different based on your OS. I’m sure you can do some investigation and find the directory.
Enjoy!
Lee
New tutorial on 3D Flash video without code
| I just uploaded a new tutorial that shows you how to create a cool 3D video piece without using any ActionScript at all. These types of things are simple for experienced AS3 developers but, up until Flash CS4, designers have been left out the fun. This tutorial starts in Photoshop where we tweak an image of a plasma TV. Then inside of Flash we use the new 3D tools to position a video component so that it plays external video files on the TV. A simple, yet cool effect. |
Note: In the tutorial I position the video component using a lot of trial and error. If anyone knows of a better way please let us know in the comments.
Lee









