views:

7273

answers:

5

I've been looking for a solution to this for years, but nothing is conclusively documented. There are many Shoutcast Flash players out there (e.g. radio.de) so I know it's possible. However, most of my research leads to this:

s = new Sound();
s.loadSound ("url.of.shoutcaststream:8003",true);

Which works for me in FireFox but not in IE. I don't want to buy a component, I want to know how those components do it so that I can build my own custom player.

A: 

If it's a stream, it's probably played through the NetStream and NetConnection classes. For example:

package {
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.Event;

    public class NetConnectionExample extends Sprite {
        private var streamURL:String = "url.of.shoutcaststream:8003";
        private var connection:NetConnection;
        private var stream:NetStream;

        public function NetConnectionExample() {
            connection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            connection.connect(null);
        }

        private function netStatusHandler(event:NetStatusEvent):void {
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found: " + streamURL);
                    break;
            }
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function connectStream():void {
            stream = new NetStream(connection);
            stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            stream.client = new CustomClient();
            stream.play(streamURL);
        }
    }
}

class CustomClient {
    public function onMetaData(info:Object):void {
        trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
    }
    public function onCuePoint(info:Object):void {
        trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
    }
}
evilpenguin
Can you provide further instructions on this code to implement it into a Flash SWF?
Jourdan
+1  A: 

You're almost there. The full mantra is:

s = new Sound();
s.loadSound ("http://url.of.shoutcaststream:8003/;",true);

Notice the trailing slash and semicolon. Shoutcast servers (DNAS) look at the useragent of a request to detect what to send back in the response. If it's a broswer then it serves a page of HTML. If it's not a browser UA, it sends the stream. Trailing semicolon (for some undocumented reason) causes DNAS to ignore the UA and always send a stream.

There's no satisfactory solution to playing AAC streams, although Flash has the equipment to do so, for some reason the API for AAC is completely different and cannot play AAC Shoutcast.

The NetStream solution here is unlikely to provide a solution.

See my blog for more info:

http://www.flexiblefactory.co.uk/flexible/?p=51

spender
A: 

Check out the player from wavestreaming.com, it's really easy to use.

http://www.wavestreaming.com/servers/flash-streaming/shoutcast-player.php

Corey
+1  A: 

The main problem doing a Stream-Player in Flash is the memory consumption.

The Flash Player keeps on recording the stream in the memory, wasting all the computer resources until it freezes, making the users very angry. :)

gyo
A: 

Hello,

I've tried to use the code provided by evilpenguin as following:

  • put the code into a "NetConnectionExample.as" file.
  • changed the streamURL to a real url.
  • and then run it with "import NetConnectionExample; var test:NetConnectionExample = new NetConnectionExample();"

I can see then that it is obviously streaming something (due to my internet activity), but I don't hear any audio. There is also no error message display in the trace output.

Does someone have an idea why I don't hear the audio although it is receiving the stream?

Thanks in advance for any idea Andreas

Andreas Breitschopp