home icon contact icon rss icon last FM icon facebook icon LinkedIn icon Delicious icon twitter icon

Archive pour le mot clé Sound

Building a streaming mp3 player

I wanted to write a post on an open source old initiative to build Flash application without any Adobe stuff : mtasc. This is for later.

For a professionnal project, I had to make a mp3 streaming player for some famous french radios. And remember I hate Flash.

I was tempted to try it with Javascript, you know, with something like :

<script>
function EvalSound(soundobj) {
  var thissound=document.getElementById(soundobj);
  thissound.Play();
}
</script>

And then :

<embed src="success.wav" autostart=false width=0 height=0 id="sound1" enablejavascript="true" />
    <form>
        <input type="button" value="Play Sound" onClick="EvalSound('sound1')">
    </form>

But even if I wasn't sure that stremed mp3 (you know, endless files) were compatible with this, some Facebook specific required features prevent me to go further. I had to use Flash.

Since I don't have any licence for using Flash IDE, I've found mtasc. And started to code the player with actionscript 2. The code to load and play a sound is pretty simple :

public class SoundPlayer {

    private var _sound:Sound;
    private var _soundUrl:String;

    public var isPlaying:Boolean = false;
    public var isLoaded:Boolean = false;

    public function SoundPlayer(soundUrl) {
        _soundUrl = soundUrl;
    }

    public function preLoadSound():Void {
        this._sound = new Sound();
        this._sound.loadSound(_soundUrl, true);
    }

    public function play():Void {
    if (!this.isLoaded) {
        this.preLoadSound();
    }

    if (!this.isPlaying) {
        this._sound.start(0, 1);
    }

        this.isPlaying = true;
    }

    public function stop():Void {
        this._sound.stop();
        this.isPlaying = false;
        this.isLoaded = false;
    }
}

And this was good. We went to test it on various computer configurations and it apear that some plugins on Internet Explorer 7 and 8 where causing the browser to freeze and relaunch. We identified this list of plugins, but I'm sure there is some others:

  • WsftpBrowserHelper Class
  • RealPLayer Downloader and record
  • AcrolEHlprObj Class

At first, I thought it was mtasc that didn't compile properly the Sound object, but I tried to compile it with the Flash IDE under the latest Flash versions (> 10) and that didn't resolve the bug.

The answer was to translate the code from ActionScript 2 to ActionScript 3. ActionScript 3 add a new Sound class and Event listener to control it. The code in ActionScript 3 is something like:

package streamPlayer {

    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;
    import flash.media.SoundLoaderContext;
    import flash.net.URLRequest;

    public class SoundPlayer {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _soundUrl:String;

        public var isPlaying:Boolean = false;
        public var isLoaded:Boolean = false;

        public function SoundPlayer(soundUrl) {
            _soundUrl = soundUrl;
        }

        public function preLoadSound() {
            this._sound = new Sound();
            var req:URLRequest = new URLRequest(_soundUrl);
            var context:SoundLoaderContext = new SoundLoaderContext(100, true);
            this._sound.load(req, context);
        }

        public function play() {
        if (!this.isLoaded) {
            this.preLoadSound();
        }

        if (!this.isPlaying) {
            this._channel = this._sound.play();
        }

            this.isPlaying = true;
        }

        public function stop() {
            var transform:SoundTransform = this._channel.soundTransform;
            transform.volume = 0;
            this._channel.soundTransform = transform;
            this._sound.close();
            this.isPlaying = false;
            this.isLoaded = false;
        }
    }
}

And it work perfectly well!