声音播放重复问题

如果有两个按钮,button1,button2,当点击button1,时播放sound1

当点击button2的时候播放sound2

当点击,比较快的时候就回发现声音重复问题

解决办法

codes

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;

var sound1:Sound = new Sound(new URLRequest("1.mp3"));
var sound2:Sound = new Sound(new URLRequest("2.mp3"));
var button1:Button;
var button2:Button;
var player:String = "";
var soundChannel:SoundChannel;
var music:Sound;


button1.addEventListener(MouseEvent.CLICK,clickHandler);
button2.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(e:MouseEvent):void
{
	trace(player);
	switch(e.currentTarget){
		case button1:
			music=sound1;
			break;
		case button2:
			music=sound2;
			break;
	}
	if (player != e.currentTarget.name)
	{

		if (player != null)
		{
			soundChannel.stop();
		}
		soundChannel = music.play();
		soundChannel.addEventListener(Event.SOUND_COMPLETE,completeHandler);
	}
	player = e.currentTarget.name;
}

function completeHandler(e:Event):void
{
	player = "";
}


你可能感兴趣的:(声音播放重复问题)