如何将 BitmapData.draw 与 NetStream.appendBytes 一起使用?

发布于 2024-10-31 18:28:33 字数 1968 浏览 9 评论 0原文

我正在使用 NetStream.appendBytes() 在 Adobe AIR 中播放本地视频(不涉及服务器) 。我想使用 BitmapData.draw() 来拍摄视频输出的图片,但出现此错误:

错误#2123:安全沙箱违规:BitmapData.draw:无法访问 null。没有授予访问权限的策略文件。

这是一些示例代码:

package
{
    import flash.display.Sprite;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.NetStreamAppendBytesAction;
    import flash.utils.ByteArray;
    import flash.display.BitmapData;

    class ByteArrayPlayer extends Sprite
    {
        private var _ns:NetStream;
        private var _nc:NetConnection;
        private var _video:Video;

        public function playVideo(path:String):void
        {
            _nc = new NetConnection();
            _nc.connect(null);
            _ns = new NetStream(_nc);

            _video = new Video();
            addChild(_video);
            _video.attachNetStream(_ns);

            _ns.play(null);
            _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);

            var file:File = new File(path);
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.READ);

            var bytes:ByteArray = new ByteArray();

            fileStream.readBytes(bytes);

            _ns.appendBytes(bytes);
        }


        public function getImage(video:Video):BitmapData
        {
            var bit:BitmapData = new BitmapData(_video.width, _video.height);
            bit.draw(_video);            //This will cause the error
            return bit;
        }
    }
}

这只是用于解释的示例代码。在视频播放过程中调用 getImage 方法时会发生该错误。该错误提到未找到策略文件。由于该文件是在本地加载的,因此实际上没有地方可以放置策略文件。是否存在需要设置的策略设置,或者使用 appendBytesBitmapData.draw 功能不可用?

I am using NetStream.appendBytes() to play a local video (no server involved) in Adobe AIR. I would like to use BitmapData.draw() to take a picture of the video output, but I am getting this error:

Error #2123: Security sandbox violation: BitmapData.draw: cannot access null. No policy files granted access.

Here is some sample code:

package
{
    import flash.display.Sprite;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.NetStreamAppendBytesAction;
    import flash.utils.ByteArray;
    import flash.display.BitmapData;

    class ByteArrayPlayer extends Sprite
    {
        private var _ns:NetStream;
        private var _nc:NetConnection;
        private var _video:Video;

        public function playVideo(path:String):void
        {
            _nc = new NetConnection();
            _nc.connect(null);
            _ns = new NetStream(_nc);

            _video = new Video();
            addChild(_video);
            _video.attachNetStream(_ns);

            _ns.play(null);
            _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);

            var file:File = new File(path);
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.READ);

            var bytes:ByteArray = new ByteArray();

            fileStream.readBytes(bytes);

            _ns.appendBytes(bytes);
        }


        public function getImage(video:Video):BitmapData
        {
            var bit:BitmapData = new BitmapData(_video.width, _video.height);
            bit.draw(_video);            //This will cause the error
            return bit;
        }
    }
}

This is only sample code use for an explanation. The error would happen when calling the getImage method while the video is playing. The error mentions a policy file not found. Since the file is loaded locally there isn't really a place to put a policy file. Is there a policy setting somewhere that needs to be set or is the BitmapData.draw feature just not available when using appendBytes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烏雲後面有陽光 2024-11-07 18:28:33

我认为另一个解决方案是使用 _ns.play(path) 而不是使用 _ns.appendBytes()

I think another solution is to use _ns.play(path) and not to use _ns.appendBytes().

孤凫 2024-11-07 18:28:33

在调用 play() 函数之前尝试设置 NetStream.checkPolicyFile = true

像这样:

_ns.checkPolicyFile = true;
_ns.play(null);

checkPolicyFile 标志的作用是告诉主机 swf 从已加载的 swf 的服务器加载策略文件。如果您在加载时未指定该标志,则当您尝试通过 BitmapData.draw() 从加载的流中获取像素数据时,您将收到 SecurityError

Adobe 资源链接:http:// help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#checkPolicyFile

Try set NetStream.checkPolicyFile = true before you call the play() function.

Like this:

_ns.checkPolicyFile = true;
_ns.play(null);

What the checkPolicyFile flag does is that it tells the host swf to load a policy file from the loaded swf's server. If you haven't specified that flag at loading time you will recieve a SecurityError when you try to get pixel data from the loaded stream through BitmapData.draw().

Link to Adobe resource: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetStream.html#checkPolicyFile

爱的那么颓废 2024-11-07 18:28:33

我有一个解决方案可以在尝试绘制 netstream.appendByte() 内容时消除沙箱错误。

SWF 检索沙箱,因为他正在您指定的地址:netstream.play(null) 中查找跨域文件。他正在寻找不存在的东西。

好吧,解决方案是播放一个假视频网址,例如“netstream.play(http://myserv.com/video.flv)”,上面带有真正的跨域文件,然后,借助appendBytes 函数,您只需加载所需的视频即可。

SWF被骗了!

尽管听起来很奇怪,但它对我有用:-)

I’ve a solution to get rid of the sandbox error when trying to draw a netstream.appendByte() content.

The SWF retrieves a sandbox because he is looking for a crossdomain file at the address specified by you :netstream.play(null). He’s looking for something that doesnt exist.

Well, the solution is to play a fake video url, like “netstream.play(http://myserv.com/video.flv)” with a genuine crossdomain file on it, and after that, you just have to load the video you want thanks to the appendBytes function.

The SWF is fooled!

As strange as it sounds, it work for me :-)

梦在深巷 2024-11-07 18:28:33

除非名为 StreamVideoSampleAccess 或 StreamAudioSampleAccess 的服务器端设置设置为 true 或像这样设置 StreamVideoSampleAccess/StramVideoSampleAccess,否则您无法从 rtmp 流中获取视频或音频的快照。我还没有找到一种方法可以在无法访问 FMS 服务器端的情况下执行此操作。

You can't take a snapshot of video or audio from an rtmp stream unless the server side settings called StreamVideoSampleAccess or StreamAudioSampleAccess is set to true or set like this StreamVideoSampleAccess/StramVideoSampleAccess. I haven't discovered a way to do this without having access to the FMS server side.

下壹個目標 2024-11-07 18:28:33

尝试将 Video 对象放置在 MovieClip 中并使用 bit.draw(_movieclipInstanceName); 而不是视频对象。我希望它能起作用。

Try placing the Video object in a MovieClip and use bit.draw(_movieclipInstanceName); instead of the video object. I hope it works.

当梦初醒 2024-11-07 18:28:33

我知道这个问题确实很老了...但这仍然是我刚刚遇到的问题,我知道其他人也会遇到...所以我想发布另一个 Stack 问题的链接,我在 Adob​​e 上发布了错误,使其正常工作的解决方法。

重复堆栈问题错误的解决方法

I know this question is really old... but it is still a problem that I just faced and I know others will too... So I wanted to post the link to the other Stack question where I posted the bug at Adobe and the workaround to get it to work.

Workaround for bug on a duplicate Stack Question

南风起 2024-11-07 18:28:33

看起来您正在尝试对未加载的视频或 NetStream 对象为 null 时调用 BitmapData.draw() 方法。

可能的修复:在调用draw()之前等待NetStream.Buffer.Full NetStatus被调度

It looks like you are trying to call the BitmapData.draw() method on a video that isn't loaded, or when the NetStream object is null.

possible fix: wait until the NetStream.Buffer.Full NetStatus is dispatched before calling draw()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文