0

如何通过按钮在 Flash/AS3 中播放/停止嵌入的音乐文件?

这就是我所拥有的:

import flash.events.Event;  
import flash.events.MouseEvent;
import flash.display.MovieClip;  
import flash.media.Sound;
import flash.media.SoundChannel;

var myMusic:Sound = new ThemeSong(); 
var channel:SoundChannel = myMusic.play();

startButton.addEventListener(MouseEvent.CLICK, onStartClick);
stopButton.addEventListener(MouseEvent.CLICK, onStopClick);

function onStartClick(event:MouseEvent):void{
    myMusic.play();
}

function onStopClick(event:MouseEvent):void{
    myMusic.stop();
}

ThemeSong 是我为音乐文件分配的类的名称(导入 -> 库 -> 属性 -> 为 AS 导出 -> 类名)。startButton 和 stopButton 是按钮的标识符。

如果我运行它,我会收到一条错误消息:

"Scene 1, Layer 'actions', Frame 1, Line 69 1061: Call to a possibly undefined method stop through a reference with static type flash.media:Sound."

谢谢。

4

1 回答 1

0

你很接近 Flash 有时很不稳定:

import flash.events.Event;  
import flash.events.MouseEvent;
import flash.display.MovieClip;  
import flash.media.Sound;
import flash.media.SoundChannel;

var myMusic:Sound = new ThemeSong(); 
var channel:SoundChannel = myMusic.play();

startButton.addEventListener(MouseEvent.CLICK, onStartClick);
stopButton.addEventListener(MouseEvent.CLICK, onStopClick);

function onStartClick(event:MouseEvent):void{
    channel = myMusic.play();
}

function onStopClick(event:MouseEvent):void{
    channel.stop();
}

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html#stop()

于 2013-05-13T04:01:41.157 回答