开发者

AAC/MP4 not working in ActionScript 3's NetStream

I'm trying to play a remote AAC file in ActionScript 3 in Flash CS3 and am currently using this code:

var url:String = "http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";

va开发者_如何学Cr connect_nc:NetConnection = new NetConnection();

connect_nc.connect(null);

var stream_ns:NetStream = new NetStream(connect_nc);

stream_ns.play(url);

(This is based on: http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html)

No errors are thrown, but no sound is played. I get the same behavior with a local AAC file and with a local MP4 video.

If I use a URL or file path that isn't a streamable file, I get a NetStream.Play.StreamNotFound error, which I'm guessing means that the stream is found in the case of a valid URL. If I use a local FLV, its audio is played just fine.

If I add the following listener and trace(evt.info.code) in netStatusHandler, I only see any codes (e.g. NetStream.Play.Start) traced with the FLV. No codes are traced with the AAC or MP4. stream_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

The same is true of adding this listener (i.e. the onMetaData argument is only traced with the FLV, not with the other file types), with metaDataListener defined as an object with an onMetaData method that traces its argument.

stream_ns.client = metaDataListener;

Any ideas of what might be going wrong here, or how to diagnose it?

Thanks!


As stated here http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html what you are doing is correct.

var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.play("RE-Sample.m4a");

However, the Actionscript Language reference about nestream found here: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/NetStream.html#play%28%29

states that:

play () method

...

...

When you use this method without Flash Media Server, there are security considerations. A file in the local-trusted or local-with-networking sandbox can load and play a video file from the remote sandbox, but cannot access the remote file's data without explicit permission in the form of a cross-domain policy file. Also, you can prevent a SWF file running in Flash Player from using this method by setting the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content.

... ...

Parameters ... arguments — The location of the video file to play, as a URLRequest object or a string. In Flash Player and in AIR content outside of the application security sandbox, you can play local video files that are stored in the same directory as the SWF file or in a subdirectory; however, you can't navigate to a higher-level directory.

So it's probably a security sandbox issue.


Everything in ActionScript 3.0 is event based (with few random exceptions where callbacks are used).

You need to listen for the NetStatusEvent with info.code "NetConnection.Connect.Success" in order to be allowed to call the NetStream.play() function.

Here's something that works (I just wrote it now, and tested it for you):

package
{
    import flash.display.Sprite;

    import flash.net.NetConnection;
    import flash.net.NetStream;

    import flash.events.NetStatusEvent;
    import flash.events.AsyncErrorEvent;
    import flash.events.Event;

    public class MainDocument extends Sprite
    {
        private var _connection:NetConnection=new NetConnection();
        private var _netStream:NetStream=null;

        private var _strM4AURL:String="http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";

        //constructor
        public function MainDocument():void
        {
            this._connect();
        }

        private function _connect():void
        {
            this._connection.close();
            this._connection=new NetConnection();
            this._connection.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
            this._connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);

            this._connection.connect(null);
        }

        private function _netStatusHandler(event:NetStatusEvent):void
        {
            trace(event.info.code);
            switch (event.info.code)
            {
                case "NetConnection.Connect.Success":
                    this._requestAudio();
                    break;
            }
        }

        private function _requestAudio():void
        {
            if(this._netStream!==null)
                this._netStream.close();

            this._netStream=new NetStream(this._connection);

            this._netStream.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
            this._netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);

            this._netStream.checkPolicyFile=false;

            this._netStream.play(this._strM4AURL);
        }

        private function _asyncErrorHandler(event:AsyncErrorEvent):void
        {
            trace(event);
        }
    }
}

Consult the ActionScript 3.0 Language Reference for more information.


There's a good chance what Oliver is saying is true as your not getting any feedback from any event listeners associated with the NetStream, and you are getting a StreamNotFound response.

StreamNotFound when not connecting to a FMS means that you either have the path wrong or its not seeing it, due to a security issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜