动作脚本添加上一些音频按钮暂停按钮 [英] action script to add a pause button on a few audio buttons

查看:209
本文介绍了动作脚本添加上一些音频按钮暂停按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用下面的code从项目库中播放的录音。

I am currently using the following code to play audio recordings from the project library.

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

var sound1:Sound = new audio1();
var sound2:Sound = new audio2();

var mySoundChannel:SoundChannel = new SoundChannel();

function stopSound():void
{
   //This stops all sound in the sound channel. 
   //If there is nothing playing, nothing happens.
   mySoundChannel.stop();
}

//In this function, we create an argument that allows us to tell the function 
//what sound to we want it to play.
function playSound(soundname:String):void
{
   try
   {
      mySoundChannel = this[soundname].play(0, 0);
   }
   catch(error:ReferenceError)
   {
      trace("playSound: That sound name doesn't exist.");
      return;
   }
 }

//Hook up buttons-->
button1.buttonMode = true;
button1.useHandCursor = true;

button2.buttonMode = true;
button2.useHandCursor = true;

button1.addEventListener(MouseEvent.CLICK, button1click);
button2.addEventListener(MouseEvent.CLICK, button2click);

function button1click(evt:Event):void
{
   stopSound();
   playSound("sound1");
}

function button2click(evt:Event):void
{
   stopSound();
   playSound("sound2");
}

我需要暂停正在播放的音频单击按钮时。如何做到这一点?

I need to pause the currently playing audio when a button is clicked. How do I do this?

推荐答案

您需要做的五件事,以你目前的code,以暂停和恢复当前正在播放的声音:

You will need to do five things to your current code in order to pause and resume the currently playing sound:

  1. 创建存储的当前播放的名称的变量 声,和一个变量来存储音频的暂停位置。
  2. 创建一个暂停功能。
  3. 创建一个恢复功能。
  4. 展开当前playSound和stopSound功能与新的变量工作。
  5. 挂接按钮事件侦听器。
  1. Create a variable that stores the name of the currently playing sound, and a variable to store the pause position of the audio.
  2. Create a pause function.
  3. Create a resume function.
  4. Expand the current playSound and stopSound functions to work with the new variables.
  5. Hook up the button event listener.

第1步:

var currentSound:String = "";
var pausePosition:Number = 0;

第二步:我们要保存在我们刚刚创建的第二个变量的音频的当前位置。我们可以使用获得该音频的当前播放位置的<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html#position"相对=nofollow> mySoundChannel.position 属性,它返回一个数字值(匹配我们给paus​​ePosition变量的数字类型)。

Step 2: We're going to save the current position of the audio in that second variable we just created. We can get the current play position of the audio using the mySoundChannel.position property, which returns a Number value (matching the Number type we gave the pausePosition variable).

function pauseSound():void
{
   //If there's a song to pause...
   if(currentSound != "")
   {
      //Get pause position.
      pausePosition = mySoundChannel.position;
      //Stop the sound directly.
      mySoundChannel.stop();
   }
}

请注意,我们没有调用stopSound()。有一个很好的理由。我们打​​算把code的额外线路在不久的功能,我们不希望在pauseSound使用()。

Note we didn't call stopSound(). There's a good reason for that. We're going to put an extra line of code in that function shortly that we don't want to use in pauseSound().

第三步:现在我们创建一个函数来恢复音频。请注意,这是不一样的playSound()。我们告诉它开始从pausePosition玩,而不是从0(声音片段的开头)。

Step 3: Now we create the function to resume audio. Note this is NOT the same as playSound(). We're telling it to start playing from pausePosition, not from 0 (the beginning of the sound clip).

function resumeSound():void
{
   //If there's a song to resume...
   if(currentSound != "")
   {
      //Start playing the current audio from the position we left off at.
      mySoundChannel = this[currentSound].play(pausePosition);
   }
}

第四步:由于我们现在正与我们在步骤1中声明这些变量,我们需要调整如何playSound()和stopSound()工作

Step 4: Since we're now working with those variables we declared in step 1, we need to adjust how playSound() and stopSound() work.

在playSound(),而不是仅仅通过soundname给声道,我们要保存soundname到currentSound。

In playSound(), instead of just passing soundname to the sound channel, we're going to save the soundname to currentSound.

function playSound(soundname:String):void
{
   try
   {
      currentSound = soundname
      mySoundChannel = this[currentSound].play(0, 0);
   }
   catch(error:ReferenceError)
   {
      trace("playSound: That sound name doesn't exist.");
      return;
   }
}

在stopSound(),我们需要真正明确currentSound和pausePosition变量,当我们停下来,确保resumeSound不启动声音后,我们已经完全停止了。

In stopSound(), we need to actually clear the currentSound and pausePosition variables when we stop, to ensure that resumeSound doesn't start audio after we've totally stopped it.

function stopSound():void
{
   //This stops all sound in the sound channel. 
   //If there is nothing playing, nothing happens.
   mySoundChannel.stop();

   //Clear our variables.
   currentSound = "";
   pausePosition = 0;
}

疑难杂症警告:一般情况下,你可以通过传递一个整数0以外的第二个参数循环音频(在那里我有5)以下code:

GOTCHA WARNING: Ordinarily, you can loop audio by passing an integer other than 0 in the second argument (where I have the 5) in the following code:

mySoundChannel = this[currentSound].play(0, 5);

在上面的code,声音会从头开始播放,重复五次。

In the above code, the sound would play from the beginning, and repeat five times.

不过,如果你开始从0以外的任何位置上的音频,会有什么实际发生的是,音频将循环在你开始在这个位置,而不是在开头。

However, if you are starting the audio from any position other than 0, what will actually happen is that the audio will loop at the position you start at, not at the beginning.

这就是说,如果你使用这个code:

That is to say, if you use this code:

mySoundChannel = this[currentSound].play(1000, 5);

声音将循环五次,但每一个声音重新开始在循环时,它会开始从位置1000打和NOT声音的开头(0)

The sound will loop five times, but every time the sound starts over in the loop, it will start playing from position 1000, and NOT the beginning of the sound (0).

我希望回答您的问题!

这篇关于动作脚本添加上一些音频按钮暂停按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆