Sunday, January 17, 2010

Flex VideoDisplay Bug and The Component Life Cycle

We ran into this strange problem at work this week, we were dealing with a Component class that uses the VideoDisplay component in Flex in order to play video. The class was unfortunately not consistent with the Flex Component life cycle guidelines. Working with the class, I wanted to be able to use the CuePointManager so I had to first set the property VideoDisplay.cuePointManagerClass. The problem was that even though I set this member, when examining the VideoDisplay instance in the debugger I got cuePointManagerClass=null. When I examined the VideoDispaly class I found the problem, the VideoDisplay code constructs the CuePointManager in a method that is called from CreateChildren the name of that method is createVideoPlayer.

1:  private function createVideoPlayer():void
2: {
3: .
4: if (cuePointManagerClass)
5: {
6: _cuePointManager = new cuePointManagerClass(videoPlayer);
7: _cuePointManager.videoDisplay = this;
8: }
9: .
10: }

The setter cuePointManagerClass simply states:

1:  public function set cuePointManagerClass(value:Class):void
2: {
3: _cuePointManagerClass = value;
4: }

In our scenario the above setter was called after the VideoDisplay class was added to the parent component, therefore, the class createChildren method followed by createVideoPlayer were already called, the setter did not invoke any invalidation method that could invoke createVideoPlayer and would make sure the cuePointManager is instantiated. Although this is a bug, we can avoid running into it if we stick to the Adobe's Component codding conventions. The component life cycle docs and code samples hints that you should first instantiate and set all members of a new child and then add it using addChild

What are the lessons to be learned here?

  1. Like any other engineering product, Flex SDK has bugs.
  2. Following Adobe's codding conventions will help you avoid running into troubles.

No comments: