2016-03-28 109 views
2

我目前正试图在flex上制作游戏,而我遇到的其中一个问题是如何在开始时播放短动画。这是我到目前为止有:使用flex播放视频,AS3

Game.mxml

<?xml version="1.0"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    name="Game" 
    backgroundColor="#000000" 
    horizontalAlign="center" 
    creationComplete="Init();" 
    enterFrame="UpdateFrame();" 
    paddingLeft="0" 
    paddingTop="0" 
    paddingBottom="0" 
    paddingRight="0" 
    width="800" height="600"> 

    <mx:Script> 
    <![CDATA[ 
    include "Game.as"; 
    ]]> 
    </mx:Script> 

    <mx:Canvas id="gamePanel" x="0" y="0" width="100%" height="100%" mouseDown="MouseDown(event)" mouseUp="MouseUp(event)" mouseMove="MouseMoved(event)"/> 
</mx:Application> 

和Game.as

import flash.display.*; 
import flash.events.*; 
import flash.external.ExternalInterface; 
import mx.events.*; 
import mx.controls.*; 

[Embed(source="MyVideoClip.flv")] private var MyVideoClip:Class; 

public function Init():void 
{ 
    var MyVideo:Video = new Video(800, 600); 
    addChild(MyVideo); 
    var qNetConnection:NetConnection = new NetConnection(); 
    qNetConnection.connect(null); 
    var qNetStream:NetStream = new NetStream(qNetConnection); 
    MyVideo.attachNetStream(qNetStream); 
    qNetStream.client = new Object(); 
    qNetStream.play(MyVideoClip); 
} 

private function UpdateFrame():void 
{ 

} 

private function MouseDown(event:MouseEvent):void 
{ 

} 

private function MouseUp(event:MouseEvent):void  
{ 

} 

private function MouseMoved(event:MouseEvent):void 
{ 

} 

我是相当新的Flex和AS3所以大多数代码被扯掉网教程。每当我尝试编译它时,我都会得到:'错误:'MyVideoClip.flv'没有延伸扩展,并且没有提供mimeType。错误:无法转码'MyVideoClip.flv''

如果我在play()函数中删除'embed'行并将MyVideoClip替换为“MyVideoClip.flv”,则代码无错误编译,但是当我打开我得到的SWF只是一个黑屏。我在做什么非常错误?

在此先感谢!

+0

编译器不知道如何转码的mime类型有限,所以将它嵌入到字节数组中,就像RobertL的答案所说的那样,你可以通过NetStream播放它...... Mime类型:http:// stackoverflow.com/questions/6012772/as3-transcoder-mimetypes – SushiHangover

回答

2

尝试设置MIME类型,例如:

[Embed(source = "MyVideoClip.flv", mimeType = "application/octet-stream")] 
+0

它现在给我“无法解析MyVideoClip.flv转码”... –

+0

.flv位于哪里?上面假定它与文档类位于同一个目录中。 –

+0

这是我与MXML和AS文件相同的目录。 “文档课程”是什么意思? –

2

您的视频文件(其字节)嵌入到输出的SWF。所以现在NetStream必须从一个字节源播放。只需将byteArray设置为new MyVideoClip();并附加到NetStream即可。

尝试......

[Embed(source="MyVideoClip.flv", mimeType="application/octet-stream")] private var MyVideoClip:Class; //# embed the FLV's bytes 

public var VideoClipBytes : ByteArray; 
public var MyVideo:Video; 

public var qNetConnection:NetConnection; 
public var qNetStream:NetStream; 

public function Init():void 
{ 
    VideoClipBytes = new MyVideoClip() as ByteArray; //# fill a byte Array with embedded bytes 

    qNetConnection = new NetConnection(); qNetConnection.connect(null); 
    qNetStream = new NetStream(qNetConnection); 
    qNetStream.client = new Object(); 

    MyVideo = new Video(800, 600); 
    MyVideo.attachNetStream(qNetStream); 
    addChild(MyVideo); 

    qNetStream.play(null); //# play mode is now bytes 
    qNetStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); //# ready for new audio/video data   
    qNetStream.appendBytes(VideoClipBytes); //# add bytes to decoder queue (playback) 

} 

PS:我做了你的一些变量如public这样以后就可以从访问&控制他们任何等功能。请记住,如果您在公共功能内制作var,它只会在该功能中保持有效,而对其他功能不存在。最好使所有功能公开可用的变量。