mp3 循环问题-Actionscript 3

发布于 2024-12-11 15:34:47 字数 2890 浏览 0 评论 0原文

这太奇怪了,这篇文章不会让我以 hello hi 或 howdy 开头,也许是缓存问题,无论如何,你好,我对 AS3 非常陌生,所以我的知识有限,希望各位专业人士能够轻松地了解新的内容家伙:)。 我正在构建一个带有循环动画的 Flash 网站,并且添加了一首 mp3 歌曲作为背景音乐,它还设置为循环和自动播放。问题是,歌曲比动画长,当动画返回到第一帧时,歌曲与仍在播放的歌曲重叠。我希望这首歌能够在再次开始播放之前完全完成。是的,可能缺少一堆代码,但我已经接近我想要的了,只需要你的帮助来完善它。

这是到目前为止的代码

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);
  }
}

This is so weird, this post wont let me start the sentence with hello hi or howdy maybe cache issue, anyways, hello, i'm very very new to AS3 so my knowledge is limited, hoping you pros can take it easy on the new guy :).
I'm building a flash site with an animation that loops and i've added an mp3 song as background music, it's also set to loop and auto play. the problem is, that the song is longer than the animation, and when the animation goes back to frame 1, the song overlaps the one that's still playing. I'd like for the song to fully finish before it starts playing again. yeaaah there's probably a heap of code missing, but i got closer to what i want, just need your help to polish it a little.

here is the code so far

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;
}
}

THIS is the full working code thanks to Josha, with an .addEventListener that i added so that the song would loop after its finished playing.

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);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-12-18 15:34:47

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

您是否将代码放在单独的 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;
  }
}

I can not place comments yet, so have to use answer.

Are you placing your code in separate AS files; or is this code placed at the first frame of your animation?

If the latter is the case, that indeed you will get overlapping sounds. Since the actionscript code is executed every time the playhead returns to frame 1; creating a new Sound object and starting to play that sound.

A simple solution is to create a key frame at the last frame and add the following action script:

gotoAndPlay(2);

This will loop your animation from frame 2, skipping over the action script.

Another solution that might work (you would have to test it):

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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文