オンラインで見つけた次のスクリプト(AS3)を使用して、フラッシュで音楽を再生しました。アクションスクリプトがわかりません。
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
function onComplete(evt:Event):void {
//Play loaded sound
sndChannel = soundClip.play();
isPlaying = true;
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}
上記のスクリプトでわかるように、music_play
はmusic_stop
再生ボタンと停止ボタンのインスタンス名です。.mp3ファイルをロードすると、音楽が再生されるはずですが、音楽はFlashから表示した場合にのみ再生されます。しかし、ローカルまたはオンライン(http://ulfhc.com.au/)で表示すると、音楽が再生されません。.swfまたは.mp3ファイルの場所が原因ではないと確信しています。
これを手伝ってくれませんか。
どうもありがとうございます。
編集:
では、新しいコードはこれでしょうか?
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
var soundClip:Sound;
function init() {
soundClip = new Sound();
soundClip.load(new URLRequest("music.mp3"));
soundClip.addEventListener(Event.COMPLETE, soundLoaded);
soundClip.addEventListener(ProgressEvent.PROGRESS, soundLoading);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
}
init();
function onComplete(evt:Event):void {
sndChannel = soundClip.play();
isPlaying = true;
}
function soundLoaded(e:Event) {
soundClip.play();
}
function soundLoading(e:ProgressEvent) {
// preloader information goes here
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}