我發現無縫循環視頻的唯一方法是將整個剪輯加載到內存ByteArray中,然後使用連接到Video實例的NetStream的appendBytes函數。
這是一個非常基本的輔助類
package
{
import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.utils.setTimeout;
/**
* @author Michael Archbold (ma@distriqt.com)
*/
public class AppendByteVideoLoop
{
public function AppendByteVideoLoop(video:Video, data:ByteArray):void
{
_video = video;
connect(data);
}
private var _video:Video;
private var _connection:NetConnection;
private var _stream:NetStream;
private var _byteArray:ByteArray;
private var _timer:Timer;
private var _paused : Boolean = false;
public function get paused():Boolean { return _paused; }
public function play():void
{
if (_stream)
_stream.resume();
}
public function pause():void
{
if (_stream)
_stream.pause();
}
public function togglePause():void
{
if (_stream)
_stream.togglePause();
}
private function connect(byteArray:ByteArray):void
{
_byteArray = byteArray;
_connection = new NetConnection();
_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
_connection.connect(null);
}
private function addToStream():void
{
_stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
_stream.appendBytes(_byteArray);
}
public function onMetaData(metaData:Object):void
{
// _video.width = metaData.width;
// _video.height = metaData.height;
}
public function onXMPData(xmp:Object):void
{
}
public function onPlayStatus(status:Object):void
{
}
private function connectStream():void
{
_stream = new NetStream(_connection);
_stream.client = this;
_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
_video.attachNetStream(_stream);
_stream.play(null);
_stream.appendBytes(_byteArray);
_stream.pause();
}
private function netStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video ");
break;
case "NetStream.Pause.Notify":
_paused = true;
break;
case "NetStream.Unpause.Notify":
_paused = false;
break;
case "NetStream.Buffer.Empty":
addToStream();
break;
case "NetStream.Buffer.Full":
break;
}
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event.text);
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler: " + event.error.message);
}
}
}
,然後用它是這樣的:
var video:Video = new Video();
video.smoothing = false;
if (stage)
{
video.width = stage.stageWidth;
video.height = stage.stageHeight;
}
addChild(video);
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler, false, 0, true);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler, false, 0, true);
loader.load(new URLRequest("URL_OF_VIDEO"));
...
function loader_completeHandler(event:Event):void
{
var data:ByteArray = ByteArray(loader.data);
var player:AppendByteVideoLoop = new AppendByteVideoLoop(_video, _data);
player.play();
}
希望有所幫助。
您可以嘗試使用seek而不是play(因爲play可能會嘗試重新加載文件)'flvControl.seek(0)'儘管可能會在事件分派時發生延遲。通常,當我這樣做時,我直接使用網絡流 - flvplayback組件將它全部抽象出來。你需要使用FLVPlayback組件嗎? – BadFeelingAboutThis
seek(0)does not help,我以前試過。我擅長使用任何可以在瀏覽器中始終如一地執行操作的技術。 (甚至HTML5)。你如何與Netstream做到這一點?我在互聯網上發現了很多代碼示例,但當我嘗試它們時,它們都有閃爍問題 – Tanuki
我相信HTML5視頻節點具有循環屬性。無論如何,你不應該在瀏覽器中使用Flash,除非它是受控制的環境。 – BadFeelingAboutThis