【问题标题】:action script to add a pause button on a few audio buttons在一些音频按钮上添加暂停按钮的操作脚本
【发布时间】:2012-10-29 02:37:41
【问题描述】:

我目前正在使用以下代码播放项目库中的录音。

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

单击按钮时,我需要暂停当前播放的音频。我该怎么做?

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    您需要对当前代码执行五项操作才能暂停和恢复当前播放的声音:

    1. 创建一个变量来存储当前播放的名称 声音,以及一个存储音频暂停位置的变量。
    2. 创建暂停功能。
    3. 创建恢复函数。
    4. 扩展当前的 playSound 和 stopSound 函数以使用新变量。
    5. 连接按钮事件监听器。

    第 1 步:

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

    第 2 步:我们将在刚刚创建的第二个变量中保存音频的当前位置。我们可以使用mySoundChannel.position 属性获取音频的当前播放位置,该属性返回一个数字值(匹配我们为 pausePosition 变量提供的数字类型)。

    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()。这是有充分理由的。我们很快将在该函数中添加一行我们不想在 pauseSound() 中使用的额外代码。

    第 3 步:现在我们创建恢复音频的函数。请注意,这与 playSound() 不同。我们告诉它从 pausePosition 开始播放,而不是从 0(声音片段的开头)开始播放。

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

    第 4 步:由于我们现在使用的是在第 1 步中声明的变量,因此我们需要调整 playSound() 和 stopSound() 的工作方式。

    在 playSound() 中,我们不只是将声音名称传递给声道,而是将声音名称保存到 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 在我们完全停止后不会启动音频。

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

    GOTCHA WARNING:通常,您可以通过在以下代码中的第二个参数(我有 5)中传递除 0 以外的整数来循环音频:

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

    在上面的代码中,声音会从头开始播放,并重复五次。

    但是,如果您从 0 以外的任何位置开始播放音频,实际发生的情况是音频将在您开始的位置循环播放,而不是在开头。

    也就是说,如果你使用这段代码:

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

    声音会循环五次,但每次声音在循环中重新开始时,都会从位置 1000 开始播放,而不是声音的开头 (0)。

    希望能回答你的问题!

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多