Why is my flash-based video player on android always crash after a few hours of play?
I'm writing a flash-based Android App. The only thing that native android part do using webview to load a flash swf. The swf acted as the container for all module (which all written in flash as3). One of the module is a simple video module which loop play a set of video playlist forever.
I've considered memory leak, but after printing memory usage (using flash's System.totalMemory), the result is always around 12MB to 14MB (which seems normal for two videos). I've test the flash using both webview and other third party swf player for android (such as "Swf Player" and "Smart SWF Player"), all results in crash after a few hours.
The as3 code is simple and I can't see any possible cause for this. Here is my main class:
import flash.display.MovieClip;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
public class simpleVid extends MovieClip {
private var video:Video;
private var nc:NetConnection;
private var ns:NetStream;
private var uri:Array = new Array("vid1.flv", "vid2.flv");
private var counter:int = 0;
public function simpleVid() {
// constructor code
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
video = new Video();
video.attachNetStream(ns);
ns.client = {onMetaData:videoReady, NetStatusEvent:onStatusEvent};
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
ns.play(uri[counter]);
stage.addChild(video);
counter++;
counter = counter % 2;
}
public function videoReady(item:Object){
video.width = 1280;
video.height = 720;
}
public function onStatusEvent(event:NetStatusEvent):void{
if (event.info.code == "NetStream.Play.Stop") {
ns.play(uri[counter]);
counter++;
counter = counter % 2;
}
}
}
Is there is anything I missed or I did wrong in this code?
Thanks in advance.
System.totalMemory
has never seemed accurate to me, likely because it does not include the actual runtime memory. If you can, get Adobe Scout and use that to profile memory and CPU usage to see if either gradually builds over time. Also remember that depending on Android device, you may not have much memory to play with and I believe there are arbitrary limits set on what an app can use in some (if not all) versions of Android) – Josh