I'm building an AS3 media player and I am having trouble loading an external movie. I keep getting the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference at CookBookPlayer$iinit()
Here is my script for the main Loader class:
Code:
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;
import com.monkeyclaus.mediaplayer.PanControl;
import com.monkeyclaus.mediaplayer.PlayButton;
import com.monkeyclaus.mediaplayer.SpectrumGraph;
import com.monkeyclaus.mediaplayer.VolumeControl;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.utils.Timer;
public class LoadPlayer extends Sprite{
private var _loader:Loader;
public function LoadPlayer(){
//Create the loader and add it to the display list
_loader = new Loader();
addChild( _loader );
//add the event handler to interact with the movie
_loader.contentLoaderInfo.addEventListener( Event.INIT, handleInit );
//Load an external movie
_loader.load (new URLRequest( "MediaPlayer.swf" ));
}
// Event Handler called when the external loaded movie is ready
private function handleInit (event:Event):void{
var movie:* = _loader.content;
trace("loaded");
}
}
}
and here is the code for the media player application I am trying to load:
Code:
package {
import com.monkeyclaus.mediaplayer.PanControl;
import com.monkeyclaus.mediaplayer.PlayButton;
import com.monkeyclaus.mediaplayer.SpectrumGraph;
import com.monkeyclaus.mediaplayer.VolumeControl;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.utils.Timer;
public class CookBookPlayer extends Sprite{
private var _channel:SoundChannel;
private var _displayText:TextField;
private var _sound:Sound;
private var _panControl:PanControl;
private var _playing:Boolean = false;
private var _playPauseButton:Sprite;
private var _position:int = 0;
private var _spectrumGraph:SpectrumGraph;
private var _volumeControl:VolumeControl;
public function CookBookPlayer() {
trace("player started");
//Stage alignment
stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
stage.align = flash.display.StageAlign.TOP_LEFT;
//Enter a frame listener
var timer:Timer = new Timer(20);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
_playing = true;
//Display a text field
_displayText = new TextField();
addChild(_displayText);
_displayText.x = 10;
_displayText.y = 17;
_displayText.width = 256;
_displayText.height = 14;
//Create a sound object
_sound = new Sound(new URLRequest("ruxpin_turtles_sea_forward.mp3"));
_sound.addEventListener(Event.ID3, onID3);
_channel = _sound.play();
//Create a bitmap for spectrum display
_spectrumGraph = new SpectrumGraph();
_spectrumGraph.x = 10;
_spectrumGraph.y = 33;
addChild(_spectrumGraph);
//Create the play and pause buttons
_playPauseButton = new PlayButton();
_playPauseButton.x = 10;
_playPauseButton.y = 68;
addChild(_playPauseButton);
_playPauseButton.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);
//Create volume and pan controls
_volumeControl = new VolumeControl();
_volumeControl.x = 45;
_volumeControl.y = 68;
addChild(_volumeControl);
_volumeControl.addEventListener(Event.CHANGE, onTransform);
_panControl = new PanControl();
_panControl.x = 164;
_panControl.y = 68;
addChild(_panControl);
_panControl.addEventListener(Event.CHANGE, onTransform);
}
public function onTransform(event:Event):void{
//Get volume and pan data from controls
// and apply to a new SoundTransform Object
_channel.soundTransform = new SoundTransform(_volumeControl.volume, _panControl.pan);
}
public function onPlayPause(event:MouseEvent):void{
//If playing, stop and record that position
if(_playing) {
_position = _channel.position;
_channel.stop();
}
else{
//Else, restart at the saved position
_channel = _sound.play(_position);
}
_playing = !_playing;
}
public function onID3(event:Event):void{
//Display selected id3 tags in the text field
_displayText.text = _sound.id3.artist + " : " + _sound.id3.songName;
_displayText.setTextFormat(new TextFormat("Verdana", 8, 0xffffff));
}
public function onTimer(event:TimerEvent):void {
var barWidth:int = 256;
var barHeight:int = 5;
var loaded:int = _sound.bytesLoaded;
var total:int = _sound.bytesTotal;
var length:int = _sound.length;
var position:int = _channel.position;
//Draw a background bar
graphics.clear();
graphics.beginFill(0xffffff);
graphics.drawRect(10, 10, barWidth, barHeight);
graphics.endFill();
if(total > 0) {
//The percent of the sound that has loaded
var percentBuffered:Number = loaded / total;
//Draw a bar that represents the percent of
// the sound that has loaded
graphics.beginFill(0xcccccc);
graphics.drawRect(10, 10, barWidth * percentBuffered, barHeight);
graphics.endFill();
//Correct the sound length calculation
length /= percentBuffered;
//The percent of the sound that has played
var percentPlayed:Number = position / length;
//Draw a bar that represents the percent of
// the sound that has played
graphics.beginFill(0xffffff);
graphics.drawRect(10, 10, barWidth * percentPlayed, barHeight);
graphics.endFill();
_spectrumGraph.update();
}
}
}
}
Any ideas? I would greatly appreciate it.