Posts Tagged 'sound'

How to check if URL is not MP3 ?

Since flash player can’t send HEAD requests, you have to resort to dirty tricks if you want to check URL content type. In this example, we are going to try and figure out if some URL points to playable sound. Since we’re dealing with unknown URL, chances are that its domain does not permit us to access raw data, so we can’t really use URLLoader – we need to actually load whatever URL points to into Sound object. However, if you thought that you could just catch i/o error 2124, like you would do with image Loader, for example, you’re in for big surprise: it does not fire. Instead, Sound object fires progress and complete events, and is even happy to play (although you will not hear any noise, or anything at all).

So, how do we detect that our Sound object is actually broken? First thing you’ll notice is that even after complete event it is still buffering. Unusual, but the same thing happens with really short sounds, so this check is not reliable. The other thing is that extract() method obviously fails to return any samples. This in itself is again not enough, since it does happen with partially loaded sounds too. However, the combination of the two seems to hit the nail:

private function onComplete(e:Event):void {
   var s:Sound = as Sound;
   if (s.isBuffering && (s.extract(new ByteArray(), 1, 0) < 1)) {
      // url does not point to playable sound

See this method in action at :)

Offline until 2010


What exactly is position in Sound.extract() ?

Does anyone know for sure? Today I have been working on simple sound queue using Sound.extract() with SampleDataEvent to avoid infamous SOUND_COMPLETE accuracy problems. For some (not really valid) reason I tried to manually track position passed to Sound.extract(), and I discovered that it is not actually measured in samples, as documentation claims. Actual units depend on sound compression method, for example in this demo below for raw 22kHz sounds I had to specify position = samples / 2:

The same demo with 16 kbps MP3 compression would have to use samples / 4. Intriguing…

Old stuff

May 2017
« Jan    

Oh, btw…