Skip to main content
May 26, 2010
Question

Playing mp3 files - MetaData?

  • May 26, 2010
  • 1 reply
  • 1480 views

We're using Amazon S3 and Cloudfront to deliver RTMPE streams to Flash player - so that we can somewhat protect the audio files. I have it working except that I cannot figure out how to get the duration of the audio. onMetaData doesn't fire at all... and the NetStream.info object only contains current data - it doesn't have the bytes loaded / total... Plus I get only 0 from NetStream.bytesTotal and bytesLoaded. I need this info to create a progress bar...

Anything I can do?

    This topic has been closed for replies.

    1 reply

    Adobe Employee
    May 27, 2010

    Hi,

    For getting duration you can use the following server side and client side code :

    Server Side code :

    main.asc

    application.onConnect = function( client ) {
        client.getStreamLength = function( streamName ) {
            trace("length is " + Stream.length( streamName ));
            return Stream.length( streamName );
        }
        application.acceptConnection( client );
    }

    Client Side code :

    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.*;

    var nc:NetConnection;
    var ns:NetStream;
    var responder:Responder;

    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, script_ncStatus);
    nc.connect("rtmp://localhost/test");
    function script_ncStatus(event:NetStatusEvent):void{
    var info:Object = event.info;
    if(info.code=="NetConnection.Connect.Success"){
      connectStream();
    }

    }

    function connectStream():void {
        ns = new NetStream(nc);
        ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        ns.client = new Object();

        responder = new Responder(onResult);
        nc.call("getStreamLength", responder, "mp3:haare_haare" );

    }

    function netStatusHandler(event:NetStatusEvent):void
    {

    }

    function onResult(result:Object):void {
      trace("The stream length is " + result + " seconds");
    }

    Regards,

    Amit

    May 27, 2010

    As I mentioned we're using Amazon's Cloudfront - I cannot add server side code. Thanks anyway though. Anyone else, or is this just not possible?

    Adobe Employee
    May 27, 2010

    Hi

    Sorry for the previous reply I couldnt get properly but I think you can use this AS3 code in Flash CS4. Your mp3 file should be present in the same directory in which your fla file is kept or else you need to modify URLRequest("haare_haare.mp3")) accordingly. Hope It might solve your problem.

    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.*;

    var mySound:Sound;

    function setup()
    {
    mySound = new Sound();
    mySound.load(new URLRequest("haare_haare.mp3"));
    mySound.addEventListener(Event.COMPLETE, soundLoaded);
    }
    setup();

    function getLength( snd:Sound ):void
    {
        var length:Number = 0;

        if( snd.bytesLoaded )
        {
            length = snd.length * ( snd.bytesTotal / snd.bytesLoaded );
        }

        trace("Length : " + length);
    }

    function soundLoaded(event:Event):void
    {
    mySound.play();
    getLength(mySound);
    }

    Regards,

    Amit