mp3循环问题 - 动作脚本3

时间:2011-10-23 22:19:04

标签: actionscript-3 loops mp3 flash-cs5

这太奇怪了,这篇文章不会让我用hello hi开始这句话或者howdy也许是缓存问题,反正,你好,我对AS3非常新,所以我的知识有限,希望你的专业人士可以放轻松在新人:)。 我正在建立一个带有动画循环的Flash网站,我已经添加了一首mp3歌曲作为背景音乐,它也设置为循环和自动播放。问题是,歌曲比动画长,当动画回到第1帧时,歌曲与正在播放的歌曲重叠。我希望这首歌在它再次开始播放之前完全完成。是的,可能有一大堆代码缺失,但我更接近我想要的东西,只需要你的帮助来稍微抛光一下。

这是迄今为止的代码

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;

//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean=false;
var pausePosition:Number=0;
var myMusic:Sound=new Sound();
var soundFile:URLRequest=new URLRequest("Fluke.mp3");
myMusic.load(soundFile);
var channel:SoundChannel;

//if commands
if (AutoPlay==true) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}

//pausebutton functions
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}

//playbutton functions
play_btn.addEventListener(MouseEvent.CLICK,playMus ic);
function playMusic(event:MouseEvent):void
{ if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}


这个是完整的工作代码,感谢Josha,我添加了一个.addEventListener,以便歌曲在完成播放后循环播放。

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;


//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;

// only create music and register event listeners one time
if (myMusic == null)
{
  soundFile = new URLRequest("Fluke.mp3");
 myMusic = new Sound();
  myMusic.load(soundFile);
 pausePosition = 0;
 if (AutoPlay) 
  {
    channel = myMusic.play(pausePosition);
isPlaying = true;
  }
  else
  {
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}

channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
pausePosition = 0;
channel = myMusic.play(pausePosition);
isPlaying = true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}


//playbutton functions
function pauseSound(e:MouseEvent):void
{
  if (isPlaying == false) {
    channel = myMusic.play(pausePosition);
    isPlaying =true;
    channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
  }else{
    pausePosition=channel.position;
    channel.stop();
    isPlaying=false;
  }
}

//playbutton functions
function playMusic(event:MouseEvent):void
{ 
  if (isPlaying== false) {
    channel=myMusic.play(pausePosition);
    isPlaying=true;
    channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
  }
}

1 个答案:

答案 0 :(得分:0)

我还不能发表评论,所以必须使用答案。

您是否将代码放在单独的AS文件中;或者此代码是否放在动画的第一帧?

如果是后者,那确实会发出重叠的声音。由于每次播放头返回到第1帧时都会执行动作脚本代码;创建一个新的Sound对象并开始播放该声音。

一个简单的解决方案是在最后一帧创建一个关键帧并添加以下动作脚本:

gotoAndPlay(2);

这将从第2帧循环播放动画,跳过动作脚本。

可能有效的另一种解决方案(您必须对其进行测试):

import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;


//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;

// only create music and register event listeners one time
if (myMusic == null)
{
  soundFile = new URLRequest("Fluke.mp3");
  myMusic = new Sound();
  myMusic.load(soundFile);
  pausePosition = 0;
  if (AutoPlay) 
  {
    channel = myMusic.play(pausePosition);
    isPlaying = true;
  }
  else
  {
    isPlaying = false;
  }
  pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
  play_btn.addEventListener(MouseEvent.CLICK, playMusic);
}

//playbutton functions
function pauseSound(e:MouseEvent):void
{
  if (isPlaying == false) {
    channel = myMusic.play(pausePosition);
    isPlaying =true;
  }else{
    pausePosition=channel.position;
    channel.stop();
    isPlaying=false;
  }
}

//playbutton functions
function playMusic(event:MouseEvent):void
{ 
  if (isPlaying== false) {
    channel=myMusic.play(pausePosition);
    isPlaying=true;
  }
}