How to stream a shoutcast radio broadcast in Flash (Shoutcast Flash Player)

只谈情不闲聊 提交于 2019-11-30 10:11:52

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

gyo

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. :)

// periodically check sound.bytesLoaded with setTimeout or setInterval, null the sound variable

MEM_MAX = 10 * 1024 * 1024
if(sound.bytesLoaded > MEM_MAX)
  { reloadSound(); flash.system.System.gc(); }

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);
    }
}

You will not be able to read metadata in Flash directly due to crossdomain issues. You are able to play the audio stream because Flash player considers that to be 'Content', but you will be unable to read the metadata because Flash player considers that to be 'Data' which is subject to cross domain policy.

You could add a crossdomain policy file to the ShoutCast server but this will be difficult for most users (you need to install a webserver on your ShoutCast server)

George Gardiner http://www.commonmode.co.uk

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!