5 Replies Latest reply on Feb 25, 2011 10:43 AM by unthinkMedia

    Playing mp3 over RTMP (Flash Media Server)

    jsd99 Level 3

      I've got some mp3 files that are served from a Flash Media Server.  I want to write a Flex application to play them.  Do I need to use NetConnection/NetStream or can I do it with just the Sound class?  I've got a sample app that can connect to the stream with NetConnection/NetStream but I don't know how to connect the netstream data to something that will actually play the sound.

       

      Will I be able to read the id3 data of the files with this?

        • 1. Re: Playing mp3 over RTMP (Flash Media Server)
          jsd99 Level 3

          I've managed to get Flash Player to play the audio from an mp3 file located at an rtmp:// url.  I am

          not sure about getting the id3 data from it though - I tried setting up an onMetaData event listener but it doesn't seem to get called.  Is this even the right way to do it?


          • 2. Re: Playing mp3 over RTMP (Flash Media Server)
            unthinkMedia

            I also need id3 data as well as duration. Does this all need to be done using server-side actionscript?

            • 3. Re: Playing mp3 over RTMP (Flash Media Server)
              jsd99 Level 3

              As far as I can tell, it's not possible.  RTMP doesn't transmit the id3 metadata.  I ended up having the client create an xml playlist with the metadata and the rtmp URL together, and using that to display.  This obviously only works if you know the tracks you're going to be streaming ahead of time.

              • 4. Re: Playing mp3 over RTMP (Flash Media Server)
                unthinkMedia Level 1

                I actually got it working , let me know if that helps


                /**

                * Create NetStream and attach it to Video Control, then play();

                *

                * @return     void

                * */

                 

                private function connectStream():void {

                 

                // Assign a new Netstream Connection

                if(_nsStream == null){

                _nsStream = new NetStream(_ncConnection);

                _nsStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

                _nsStream.addEventListener( AsyncErrorEvent.ASYNC_ERROR, handleCatchAllErrors );

                _nsStream.addEventListener( IOErrorEvent.IO_ERROR, handleCatchAllErrors );

                _nsStream.client = {onMetaData:handleMetaData, close:handleCloseStream};

                _nsStream.bufferTime = _videoData.buffer;

                }

                 

                // Assign a new ID3 Netstream Connection

                if( _id3_ns == null && _videoData.mediaType=="mp3") {

                _id3_ns = new NetStream(_ncConnection);

                _id3_ns.addEventListener( AsyncErrorEvent.ASYNC_ERROR, handleCatchAllErrors );

                _id3_ns.addEventListener( IOErrorEvent.IO_ERROR, handleCatchAllErrors );

                _id3_ns.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );

                 

                //HACK: we are required to pass in static duration via XML till we get server-side actionscript working

                _metadata.duration=_videoData.duration;

                 

                _id3_ns.play(_id3_path);

                _id3_ns.client = {onId3:handleOnId3};

                 

                _id3Field = new TextField();

                _id3Field.width = _video.width;

                _id3Field.wordWrap = true;

                _id3Field.multiline = true;

                _id3Field.antiAliasType = flash.text.AntiAliasType.ADVANCED;

                //_id3Field.defaultTextFormat = _ccTextFormat;

                _id3Field.selectable = false;

                this.addChild(_id3Field);

                }

                 

                _video.smoothing = _playerData.smoothing;

                _video.attachNetStream(_nsStream);

                 

                _nsStream.play(_videoData.file);

                _isPlaying = true;

                createSoundObject();

                 

                this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

                trace("[MEDIA FILE] "+_videoData.file);

                }


                /**

                * Get MP3 data from ID3 tag

                *

                * @return void

                * */

                private function handleOnId3( obj:Object ):void{

                if(obj["artist"] != undefined){

                _id3Field.text += "by: "+obj["artist"].toString() + "\n";

                 

                }

                if(obj["songtitle"] != undefined){

                _id3Field.text += "title: "+obj["songtitle"].toString();

                }

                //if we wan to inject media with other data, add it here

                for( var b:String in obj ) {

                //_id3Field.text += b + ": " + obj[ b ];

                //trace(b + ": " + obj[ b ]);

                }

                }

                • 5. Re: Playing mp3 over RTMP (Flash Media Server)
                  unthinkMedia Level 1

                  I actually got it working , let me know if that helps

                   

                   

                     /**

                   

                   

                  • Create NetStream and attach it to Video Control, then play();

                   

                  *

                   

                   

                  • @return void

                   

                   

                  • */

                   

                    private function connectStream():void {

                   

                     // Assign a new Netstream Connection

                   

                  if(_nsStream == null){

                   

                    nsStream = new NetStream(ncConnection);

                   

                    nsStream.addEventListener(NetStatusEvent.NETSTATUS, netStatusHandler);

                   

                    nsStream.addEventListener( AsyncErrorEvent.ASYNCERROR,

                  handleCatchAllErrors );

                   

                    nsStream.addEventListener( IOErrorEvent.IOERROR, handleCatchAllErrors );

                   

                    _nsStream.client = {onMetaData:handleMetaData, close:handleCloseStream};

                   

                    _nsStream.bufferTime = _videoData.buffer;

                   

                  }

                   

                     // Assign a new ID3 Netstream Connection

                   

                  if( id3ns == null && _videoData.mediaType=="mp3") {

                   

                    id3ns = new NetStream(_ncConnection);

                   

                    id3ns.addEventListener( AsyncErrorEvent.ASYNC_ERROR,

                  handleCatchAllErrors );

                   

                    id3ns.addEventListener( IOErrorEvent.IO_ERROR, handleCatchAllErrors );

                   

                    id3ns.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );

                   

                      //HACK: we are required to pass in static duration via XML till we get

                  server-side actionscript working

                   

                    metadata.duration=videoData.duration;

                   

                      id3ns.play(_id3_path);

                   

                    id3ns.client = {onId3:handleOnId3};

                   

                      _id3Field = new TextField();

                   

                    _id3Field.width = _video.width;

                   

                    _id3Field.wordWrap = true;

                   

                    _id3Field.multiline = true;

                   

                    _id3Field.antiAliasType = flash.text.AntiAliasType.ADVANCED;

                   

                    //_id3Field.defaultTextFormat = _ccTextFormat;

                   

                    _id3Field.selectable = false;

                   

                    this.addChild(_id3Field);

                   

                  }

                   

                     _video.smoothing = _playerData.smoothing;

                   

                  video.attachNetStream(nsStream);

                   

                      nsStream.play(videoData.file);

                   

                    _isPlaying = true;

                   

                    createSoundObject();

                   

                     this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

                   

                  trace("[MEDIA FILE] "+_videoData.file);

                   

                  }

                   

                     /**

                   

                   

                  • Get MP3 data from ID3 tag

                   

                  *

                   

                   

                  • @return void

                   

                   

                  • */

                   

                  private function handleOnId3( obj:Object ):void{

                   

                  if(obj["artist"] != undefined){

                   

                    _id3Field.text = "by: "obj["artist"].toString() + "\n";

                   

                      }

                   

                  if(obj["songtitle"] != undefined){

                   

                    _id3Field.text = "title: "obj["songtitle"].toString();

                   

                  }

                   

                  //if we wan to inject media with other data, add it here

                   

                  for( var b:String in obj ) {

                   

                    //_id3Field.text += b + ": " + obj[ b ];

                   

                    //trace(b + ": " + obj[ b ]);

                   

                  }

                   

                  }