PDA

View Full Version : Video player memory leak help?


Dholem
11-11-2010, 03:39 PM
Hey guys,

I'm trying to create a video player app which gets its playlist from an external xml and its going well but I'm getting some serious memory leaks.

Here is my xml playlist:

<?xml version="1.0" encoding="utf-8"?>
<playlist>
<mp4 label="TestMovie1.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie2.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie3.mp4" icon="myMovieIcon" />
<mp4 label="TestMovie4.mp4" icon="myMovieIcon" />
</playlist>

My main code:

import flash.events.Event;

[Bindable]
public var playlist:String; //"region1"

[Bindable]
public var currentItem:Number = 0;

private var dataXML:XML;
private var contentList:Array = new Array();
private var workDir:String = "c:/temp/";

private function loadXml():void
{
var loader:URLLoader = new URLLoader(new URLRequest(workDir + playlist + ".xml"));
loader.addEventListener("complete", parseXml);
}

private function parseXml(e:Event):void
{
dataXML = XML(e.target.data);
var listLength:Number = dataXML.children().length();
for(var i:Number = 0; i<listLength; i++)
{
contentList.push(dataXML.children()[i]..@label);
}

nextItem();
}

private function nextItem():void
{
var dataFile:File = new File(workDir + contentList[currentItem]);

if(dataFile.exists)
{
if(dataFile.extension == "mp4" || dataFile.extension == "flv")
{
var vp:videoPlayer = new videoPlayer();

this.addElement(vp);
vp.addEventListener(Event.COMPLETE, movieDone);
vp.x = 0;
vp.y = 0;
vp.width = this.width;
vp.height = this.height;
vp.vidName = workDir + contentList[currentItem];
vp.vidWidth = vp.width;
vp.vidHeight = vp.height;
vp.vidDis.scaleMode = "letterbox";
}
}
else
{
trace(contentList[currentItem] + " can't be located");
}

}

public function movieDone(e:Event):void
{
this.removeAllElements();

if(currentItem == contentList.length -1)
{
currentItem = 0;
nextItem();
}
else
{
currentItem = currentItem + 1;
nextItem();
}
}

And the videoPlayer component:

<s:Group>

<fx:Metadata>
[Event(name="complete", type="flash.events.Event")]
</fx:Metadata>

<fx:Script>
<![CDATA[

import org.osmf.events.TimeEvent;

[Bindable]
public var vidName:String;
[Bindable]
public var vidWidth:Number;
[Bindable]
public var vidHeight:Number;


protected function videoplayer1_completeHandler(event:TimeEvent):void
{
var e:Event = new Event("complete");
dispatchEvent(e);
}

]]>
</fx:Script>
<mx:Container x="0" y="0" width="{vidWidth}" height="{vidHeight}" backgroundColor="#000000">
<s:VideoDisplay id="vidDis" x="0" y="0" width="{vidWidth}" height="{vidHeight}" source="{vidName}" complete="videoplayer1_completeHandler(event)" chromeColor="#000000"/>
</mx:Container>
</s:Group>

So I create the videoPlayer component and pass in the mp4 file from an array. Once the video is complete it dispatches a custom event which runs the movieDone function. I call the this.removeAllElements() to remove the videoPlayer component before adding it again. I am very new at this but it doesnt seem like the video is being removed as the physical memory climbs with each new video, and eventually after several loops the program will crash. Anyone know what I'm doing wrong?

tadster
11-11-2010, 07:24 PM
what about the Array are you clearing that?

edit:

wait, I think you should be able to do it without re-instantiating the videoPlayer every time. That's what is building up, even when you remove it, you need to only call
new videoPlayer(); once, preferably in a constructor.

daslicht
04-21-2011, 12:55 PM
>"c:/temp/"
I think you cant load file directly (sandbox limitation) from the file system, except when creating a AIR application.

drkstr
04-22-2011, 01:12 AM
I 2nd Tadster. You want to swap your video source, not your video player.

cpthk
04-29-2011, 11:15 AM
I don't think you can memory leak in actionscript

drkstr
04-29-2011, 02:48 PM
I don't think you can memory leak in actionscript
You most certainly can. Any object that still has a reference to it will never be garbage collected. The most common cause of this is failing to remove event listeners.