同步声音
我的声音似乎不同步,播放影片剪辑的时间越长,它的不同步程度就越远。它没什么太复杂的,只是一些基本的射击声音,每次我按空格键时都会发出声音。我的代码如下:
package com.objects{
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
public class Avatar extends gameObject implements IKiller{
public var targets:Array;
public var delay:Number = 3000;
public var weapon:Number = 1;
private var fireSound:Sound;
private var fireSound2:Sound;
private var systems:Array;
private var ext:Number = -1;
private var channelTracer:SoundChannel;
private var channelCount:Number = 0;
public function Avatar():void
{
systems = new Array();
channelTracer = new SoundChannel();
var soundUrl = new URLRequest("com/images/mashingun_darkt.mp3");
fireSound = new Sound();
fireSound.load(soundUrl);
var soundUrl = new URLRequest("com/images/mashingun_light.mp3");
fireSound2 = new Sound();
fireSound2.load(soundUrl);
rotation = -90;
lastTime = getTime();
targets = new Array();
}
private function soundFinished(e:Event):void {
channelCount--;
trace(channelCount);
}
public function get Systems():Array
{
return systems;
}
override public function Attack(dir:Number = -40):void
{
switch(weapon){
case 1:
var bullet1:Bullet = new Bullet();
bullet1.wielder = this;
bullet1.x = x + 35;
bullet1.y = y + 30;
bullet1.bulletDir = rotation;
eApi.addGameChild(bullet1);
var bullet2:Bullet = new Bullet();
bullet2.bulletDir = rotation;
bullet2.wielder = this;
bullet2.x = x - 35;
bullet2.y = y + 30;
eApi.addGameChild(bullet2);
var rand = Math.ceil(Math.random() * 100);
if (rand < 50) {
channelTracer = fireSound.play();
//RndSound = RndSound+50;
} else {
channelTracer = fireSound2.play();
}
channelTracer.addEventListener(Event.SOUND_COMPLETE, soundFinished);
channelCount++;
trace(channelCount);
break;
case 2:
if((getTime() - lastTime) > delay)
{
var missle = new Missile();
missle.shootOut *= ext;
ext *= -1;
missle.x = x;
missle.y = y;
trace(ext);
missle.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle);
lastTime = getTime();
}
break;
case 3:
var bullet1:Bullet = new Bullet();
bullet1.wielder = this;
bullet1.x = x + 35;
bullet1.y = y + 30;
bullet1.bulletDir = -80;
eApi.addGameChild(bullet1);
var laser = new StingerLaser();
laser.laserDir = -90;
laser.wielder.push(this);
laser.x = x + 20;
laser.y = y + -3;
eApi.addGameChild(laser);
var laser2 = new StingerLaser();
laser2.laserDir = -90;
laser2.wielder.push(this);
laser2.x = x + -20;
laser2.y = y + -3;
eApi.addGameChild(laser2);
var bullet2:Bullet = new Bullet();
bullet2.bulletDir = -100;
bullet2.wielder = this;
bullet2.x = x - 35;
bullet2.y = y + 30;
eApi.addGameChild(bullet2);
break;
case 4:
if((getTime() - lastTime) > delay)
{
var missle1 = new Missile();
missle1.shootOut = 2;
missle1.x = x;
missle1.y = y;
missle1.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle1);
var missle2 = new Missile();
missle2.shootOut = -2;
missle2.x = x;
missle2.y = y;
missle2.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle2);
lastTime = getTime();
}
break;
case 5:
if((getTime() - lastTime) > delay)
{
var side:Number = 1;
for(var i = 0; i < 20; i+=2)
{
var missle1 = new Missile();
missle1.shootOut = (i+2) * side;
side *= -1;
missle1.straightShot = true;
missle1.x = x;
missle1.y = y;
missle1.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle1);
}
lastTime = getTime();
}
break;
default:
}
}
public function Hit(dmg:Number = .01):void {
if(health > 0)
health -= dmg;
if(health < 0)
health = 0,trace("dead");
}
override public function updateObject():void
{
}
}
}
Attack() 方法是从处理所有键盘控件的另一个类调用的。当它被调用时,声音就会播放。 firesound 和 firesound2 几乎相同。 firesound2 听起来有点偏离音调,以使其听起来更真实。起初,声音听起来还不错,但不是很好。但随着时间的推移,情况会变得很糟糕。不确定这是否是一个已知问题。但如果有人有任何想法,请告诉我。谢谢!
我创建了一个新的 .fla 项目。我自己将以下课程附加到它上面。所以以下是整个.fla项目中唯一的代码,问题仍然存在。我按空格键,声音晚了一天才开始。
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class test extends MovieClip
{
private var fireSound:Sound;
private var fireSound2:Sound;
private var aKeyPress:Array;
public function test():void
{
aKeyPress = new Array();
var soundUrl = new URLRequest("com/images/mashingun_darkt.mp3");
fireSound = new Sound();
fireSound.load(soundUrl);
var soundUrl = new URLRequest("com/images/mashingun_light.mp3");
fireSound2 = new Sound();
fireSound2.load(soundUrl);
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
private function keyDownListener(e:KeyboardEvent) {
//trace("down e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=true;
}
private function keyUpListener(e:KeyboardEvent) {
//trace("up e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=false;
}
public function loop(e:Event):void
{
if (aKeyPress[32]){//Right
var rand = Math.ceil(Math.random() * 100);
if (rand < 50) {
fireSound.play();
//RndSound = RndSound+50;
} else {
fireSound2.play();
}
}
}
}
}
It appears my sound is off sync and the longer I play the movieclip, the farther off sync it goes. Its nothing too complicated, just some basic shooting sounds that fire every time I hit the space bar. My code is below:
package com.objects{
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
public class Avatar extends gameObject implements IKiller{
public var targets:Array;
public var delay:Number = 3000;
public var weapon:Number = 1;
private var fireSound:Sound;
private var fireSound2:Sound;
private var systems:Array;
private var ext:Number = -1;
private var channelTracer:SoundChannel;
private var channelCount:Number = 0;
public function Avatar():void
{
systems = new Array();
channelTracer = new SoundChannel();
var soundUrl = new URLRequest("com/images/mashingun_darkt.mp3");
fireSound = new Sound();
fireSound.load(soundUrl);
var soundUrl = new URLRequest("com/images/mashingun_light.mp3");
fireSound2 = new Sound();
fireSound2.load(soundUrl);
rotation = -90;
lastTime = getTime();
targets = new Array();
}
private function soundFinished(e:Event):void {
channelCount--;
trace(channelCount);
}
public function get Systems():Array
{
return systems;
}
override public function Attack(dir:Number = -40):void
{
switch(weapon){
case 1:
var bullet1:Bullet = new Bullet();
bullet1.wielder = this;
bullet1.x = x + 35;
bullet1.y = y + 30;
bullet1.bulletDir = rotation;
eApi.addGameChild(bullet1);
var bullet2:Bullet = new Bullet();
bullet2.bulletDir = rotation;
bullet2.wielder = this;
bullet2.x = x - 35;
bullet2.y = y + 30;
eApi.addGameChild(bullet2);
var rand = Math.ceil(Math.random() * 100);
if (rand < 50) {
channelTracer = fireSound.play();
//RndSound = RndSound+50;
} else {
channelTracer = fireSound2.play();
}
channelTracer.addEventListener(Event.SOUND_COMPLETE, soundFinished);
channelCount++;
trace(channelCount);
break;
case 2:
if((getTime() - lastTime) > delay)
{
var missle = new Missile();
missle.shootOut *= ext;
ext *= -1;
missle.x = x;
missle.y = y;
trace(ext);
missle.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle);
lastTime = getTime();
}
break;
case 3:
var bullet1:Bullet = new Bullet();
bullet1.wielder = this;
bullet1.x = x + 35;
bullet1.y = y + 30;
bullet1.bulletDir = -80;
eApi.addGameChild(bullet1);
var laser = new StingerLaser();
laser.laserDir = -90;
laser.wielder.push(this);
laser.x = x + 20;
laser.y = y + -3;
eApi.addGameChild(laser);
var laser2 = new StingerLaser();
laser2.laserDir = -90;
laser2.wielder.push(this);
laser2.x = x + -20;
laser2.y = y + -3;
eApi.addGameChild(laser2);
var bullet2:Bullet = new Bullet();
bullet2.bulletDir = -100;
bullet2.wielder = this;
bullet2.x = x - 35;
bullet2.y = y + 30;
eApi.addGameChild(bullet2);
break;
case 4:
if((getTime() - lastTime) > delay)
{
var missle1 = new Missile();
missle1.shootOut = 2;
missle1.x = x;
missle1.y = y;
missle1.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle1);
var missle2 = new Missile();
missle2.shootOut = -2;
missle2.x = x;
missle2.y = y;
missle2.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle2);
lastTime = getTime();
}
break;
case 5:
if((getTime() - lastTime) > delay)
{
var side:Number = 1;
for(var i = 0; i < 20; i+=2)
{
var missle1 = new Missile();
missle1.shootOut = (i+2) * side;
side *= -1;
missle1.straightShot = true;
missle1.x = x;
missle1.y = y;
missle1.wielder = this;
eApi.addGameChildAt((eApi.numChildren - 2),missle1);
}
lastTime = getTime();
}
break;
default:
}
}
public function Hit(dmg:Number = .01):void {
if(health > 0)
health -= dmg;
if(health < 0)
health = 0,trace("dead");
}
override public function updateObject():void
{
}
}
}
The Attack() method gets called from another class that handles all keyboard controls. when it gets called the sound then plays. firesound and firesound2 are almost the same. firesound2 sounds a little off pitch to make it sound more realistic. At first the sound sounds pretty good, not great. but then it gets terrible as time passes. Not sure if this is a known issue. But if anyone has any ideas, let me know. thanks!
I made a new .fla project. and I attached the following class to it by it self. so the following is the only code in the entire .fla project and the issue still occurs. I press the spacebar and the sound starts a day late.
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class test extends MovieClip
{
private var fireSound:Sound;
private var fireSound2:Sound;
private var aKeyPress:Array;
public function test():void
{
aKeyPress = new Array();
var soundUrl = new URLRequest("com/images/mashingun_darkt.mp3");
fireSound = new Sound();
fireSound.load(soundUrl);
var soundUrl = new URLRequest("com/images/mashingun_light.mp3");
fireSound2 = new Sound();
fireSound2.load(soundUrl);
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
private function keyDownListener(e:KeyboardEvent) {
//trace("down e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=true;
}
private function keyUpListener(e:KeyboardEvent) {
//trace("up e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=false;
}
public function loop(e:Event):void
{
if (aKeyPress[32]){//Right
var rand = Math.ceil(Math.random() * 100);
if (rand < 50) {
fireSound.play();
//RndSound = RndSound+50;
} else {
fireSound2.play();
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是否有可能与同时播放的声音数量有关?
每次调用 Sound.play() 都会返回一个新的 SoundChannel 对象(一次只能拥有 32 个)。您可以像这样监控通道的使用情况:
这可能会帮助您查明问题。
编辑:我更改了上面的代码,以保持同时播放的声音数量的运行计数(我希望 - 抱歉,您必须为我测试它!)
Any chance this could be something to do with the number of concurrent sounds playing?
Each call to Sound.play() returns a new SoundChannel object (you can only have 32 of these at a time). You could monitor how the channels are being used like this:
This might help you pinpoint the problem.
EDIT: I changed the code above to keep a running count of how many sounds are playing concurrently (I hope - sorry you'll have to test it for me!)
我使用此测试代码和我自己的声音文件测试了构建,但无法遇到声音滞后问题。它可能是您的声音文件,但据我所知,没有任何问题会导致对 Sound.play() 的调用开始滞后。
我用来测试的声音文件很短(马里奥火球和跳跃声音),并且无法获得超过 4 个并发 SoundChannels 使用。没有延迟问题。
使用更长的声音文件,我能够将 SoundChannel 的数量增加到 32,此时通道跟踪器代码开始抛出空引用错误(正如预期的那样,因为 Sound.play() 无法返回更多 SoundChannel),并且听起来非常糟糕,但是一旦声音完成,计数就会减少到零。在连续两分钟惩罚我的耳机和声卡之后,我仍然没有遇到延迟。
我建议在整个代码中插入对 getTimer() 的调用,以查看它是否是性能瓶颈(即 Flash Player 实际上没有快速响应事件)或声音延迟问题。此外,您可能需要尝试不同的声音文件以查看问题是否仍然出现。
I tested a build using this test code and my own sound files, and am unable to encounter the sound lagging issue. It could be your sound files, though there are no issues that I am aware of that would cause calls to Sound.play() to begin lagging.
The sound files I used to test were short (Mario fireball and jump sounds), and was unable to get more than 4 concurrent SoundChannels in use. No latency issues.
Using longer length sound files, I was able to push the number of SoundChannels to 32, at which point the channel tracer code began throwing null reference errors (as would be expected since Sound.play() cannot return more SoundChannels), and it sounded pretty awful, but once the sounds completed the count decremented to zero. After two straight minutes of punishing my headphones and soundcard, I still encountered no latency.
I'd suggest inserting calls to getTimer() throughout your code to see if it's a performance bottleneck (i.e. Flash Player not actually responding to events quickly) or a sound latency issue. Also, you may want try different sound files to see if the issue still occurs.