如何播放音乐曲目在AS3? [英] How to play music tracks in AS3?
问题描述
我需要打三个音乐曲目一前一后在一个圆圈。必要的是其不再次加载并且是在高速缓存中。 我用这个code。一切工作正常在本地主机上,但只后,重新启动服务器上的应用程序。而错误的重新下载跟踪每一次。
I need to play three musical tracks one after the other in a circle. It is necessary that its are not loaded again and were in the cache. I use this code. Everything works fine on localhost, but only works after restart the app on the server. And wrong to re-download tracks every time.
public function musicOn ():void{
if (sndStart == 'true'){
req = new URLRequest("media/" + track + ".mp3");
snd.load(req);
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
sndStart = 'false';
} else {
sndStart = 'true';
}
}
public function musicOff ():void{
if (snd.length>0){
channel.stop();
snd = new Sound();
channel = new SoundChannel();
sndStart = 'true';
}
}
public function onPlaybackComplete(event:Event):void
{
if (track==3){
track = 1;
} else {
track++;
}
sndStart = 'true';
snd = new Sound();
musicOn();
}
我运行这些功能:
I run these functions:
if (optObj.music == 'true' && sndStart == 'true'){
musicOn();
} else if (optObj.music == 'false'){
musicOff();
}
optObj.music - 这是被称为当应用程序启动或当我调用一个函数更改设置的参数对象
optObj.music - it is the object with the parameters that is called when the app starts or when i call a function change the settings.
推荐答案
我不知道我理解你写的一切,但这个怎么样:
I'm not sure I understand everything you wrote, but how about this one:
package test
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundTest extends MovieClip
{
private var files : Array = [ "file1.mp3", "file2.mp3", "file3.mp3" ];
private var sounds : Array = [];
private var count : int = 0;
private var channel : SoundChannel;
public function musicOff () : void
{
if (channel != null)
{
channel.removeEventListener( Event.SOUND_COMPLETE, onSoundComplete );
channel.stop( );
channel = null;
}
}
public function musicOn () : void
{
if (sounds[count] == null) loadSound( );
else playLoadedSound( );
}
private function loadSound () : void
{
var sound : Sound = new Sound( );
sound.addEventListener( Event.COMPLETE, onSoundLoaded );
sound.load( new URLRequest( files[count] ) );
}
private function playLoadedSound () : void
{
channel = sounds[count].play( );
channel.addEventListener( Event.SOUND_COMPLETE, onSoundComplete );
}
private function onSoundComplete (ev : Event) : void
{
channel = null;
count++;
if (count >= files.length) count = 0;
musicOn( );
}
private function onSoundLoaded (ev : Event) : void
{
var snd : Sound = ev.target as Sound;
sounds.push( snd );
channel = snd.play( );
channel.addEventListener( Event.SOUND_COMPLETE, onSoundComplete );
}
public function SoundTest ()
{
musicOn( );
}
}
}
(顺便说一句,您可以直到被加载它播放声音。这就是为什么你的脚本只能重新启动后,当MP3在浏览器的缓存。)
(btw You can't play the Sound until it is loaded. That's why your script works only after restart, when the mp3 is in the browser cache.)
这篇关于如何播放音乐曲目在AS3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!