我收到错误 1026,即“构造函数必须是实例方法”,我不知道为什么。我正在创建一个媒体播放器类型的程序,但我的音乐部分有问题。在此之前我制作了一个功能音乐播放器并将代码复制到我的新项目中。现在唯一不同的是代码处于状态而不是 main.mxml 文件。这是我下面的代码,我收到错误消息,上面写着“public var music:Sound;” 我不明白为什么我会收到此错误,任何帮助将不胜感激!
<fx:Declarations>
<fx:XML id="musicXML" source="data/musak.xml" />
<s:XMLListCollection id="musicCollection" source="{musicXML.song}" />
</fx:Declarations>
<s:HGroup>
<s:DataGrid id="musicGrid" dataProvider="{musicCollection}" click="onClick()" />
<local:MusicPosition id="mProgress" width="319" height="83"/>
<s:VGroup height="55">
<s:Label text="Now playing: {musicGrid.selectedItem.@name}" />
<s:Label id="txtPosition" width="91"/>
</s:VGroup>
</s:HGroup>
<s:Button x="146" y="93" label="play" click="playMusic()"/>
<s:Button x="270" y="93" label="pause" click="pauseMusic()"/>
<fx:Script>
<![CDATA[
//set your variables
public var music:Sound;
[Bindable]public var musicChannel:SoundChannel;
public var pausedTime:Number = 0;
public function onClick():void {
if(musicChannel) {
musicChannel.stop();
//clean up the variables
music = null;
musicChannel = null;
}
music = new Sound();
music.load(new URLRequest(musicGrid.selectedItem.file.toString()));
music.addEventListener(Event.COMPLETE, onMusicLoad);
}
public function onMusicLoad(e:Event):void {
mProgress.update(0);
//new channel
musicChannel = music.play();
}
protected function onE(e:Event):void {
if(musicChannel) {
txtPosition.text = millisecondsToSeconds(musicChannel.position).toString() + "/" +
millisecondsToSeconds(music.length).toString();
mProgress.update(musicChannel.position/music.length);
mProgress.alpha = 1;
} else
mProgress.alpha = 0;
}
protected function millisecondsToSeconds(milliseconds:Number):Number {
return Math.round(milliseconds/1000);
}
public function pauseMusic():void {
if(musicChannel) {
pausedTime = musicChannel.position;
musicChannel.stop();
}
}
public function playMusic():void {
if(musicChannel) {
musicChannel = music.play(pausedTime);
}
}
]]>
</fx:Script>