如何播放音乐曲目在AS3?

编程入门 行业动态 更新时间:2024-10-26 02:33:13
本文介绍了如何播放音乐曲目在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.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?

本文发布于:2023-07-11 09:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:曲目   音乐

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!