创建声音控件

可以使用内置 Sound 类控制 SWF 文件中的声音。若要使用 Sound 类的方法,必须先创建一个 Sound 对象。然后可以使用 attachSound() 方法在 SWF 文件运行时将库中的声音插入该 SWF 文件。

若要查看声音控件的动画演示,可单击"播放"按钮,然后调节音量和左右平衡。

Sound 类的 setVolume() 方法控制着音量,而 setPan() 方法则调节声音的左右平衡。

以下过程演示如何创建声音控制。

将声音附加到时间轴上:

  1. 选择"文件">"导入"来导入一种声音。
  2. 在库中选择声音,右键单击 (Windows) 或按住 Control 键单击 (Macintosh),然后选择"链接"。 
  3. 选择"为 ActionScript 导出"和"在第一帧导出",然后指定声音标识符 a_thousand_ways
  4. 在舞台上添加一个按钮,然后将它命名为 play_btn
  5. 在舞台上添加一个按钮,然后将它命名为 stop_btn
  6. 在主时间轴上选择第 1 帧,然后选择"窗口">"动作"。

    将以下代码添加到"动作"面板中:

    var song_sound:Sound = new Sound();
    song_sound.attachSound("a_thousand_ways");
    play_btn.onRelease = function() {
        song_sound.start();
    };
    stop_btn.onRelease = function() {
        song_sound.stop();
    };
    

    该代码首先停止扬声器影片剪辑。然后创建一个新的 Sound 对象 (song_sound),并向该对象附加链接标识符为 a_thousand_ways 的声音。与 playButtonstopButton 对象关联的 onRelease 事件处理函数通过使用 Sound.start()Sound.stop() 方法启动和停止该声音,并且还播放和停止附加的声音。

  7. 选择"控制">"测试影片"来试听声音。

创建滑动音量控件:

  1. 使用"矩形"工具,在舞台上绘制一个小矩形,大约 30 像素高 10 像素宽。
  2. 选择"选择"工具并双击舞台上的形状。
  3. 按 F8 键打开"转换为元件"对话框。
  4. 选择"按钮"类型,输入 volume 作为元件名称并单击"确定"。
  5. 在舞台上选中该按钮元件,在"属性"检查器中输入实例名称 handle_btn
  6. 选择该按钮,然后选择"修改">"转换为元件"。

    选择影片剪辑行为时要小心。这将创建一个在第一帧中带有按钮的影片剪辑。

  7. 选择该影片剪辑,然后在属性检查器中输入 volume_mc 作为实例名称。
  8. 在主时间轴上选择第 1 帧,然后选择"窗口">"动作"。
  9. 在"动作"面板中输入下面的代码:
    this.createTextField("volume_txt", 10, 30, 30, 200, 20);
    volume_mc.top = volume_mc._y;
    volume_mc.bottom = volume_mc._y;
    volume_mc.left = volume_mc._x;
    volume_mc.right = volume_mc._x + 100;
    volume_mc._x += 100;
    
    volume_mc.handle_btn.onPress = function() {
        startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
    };
    volume_mc.handle_btn.onRelease = function() {
        stopDrag();
        var level:Number = Math.ceil(this._parent._x - this._parent.left);
        this._parent._parent.song_sound.setVolume(level);
        this._parent._parent.volume_txt.text = level;
    };
    volume_mc.handle_btn.onReleaseOutside = slider_mc.handle_btn.onRelease;
    

    startDrag() 参数 lefttoprightbottom 是在影片剪辑动作中设置的变量。

  10. 选择"控制">"测试影片"来使用音量滑块。

创建滑动的平衡控件:

  1. 使用"矩形"工具在舞台上绘制一个小矩形,大约 30 像素高 10 像素宽。
  2. 选择"选择"工具并双击舞台上的形状。
  3. 按 F8 键启动"转换为元件"对话框。
  4. 选择"按钮"类型,输入元件名称 balance 并单击"确定"。
  5. 在舞台上选中该按钮元件,在"属性"检查器中输入实例名称 handle_btn
  6. 选择该按钮,然后选择"修改">"转换为元件"。

    选择影片剪辑行为时要小心。这将创建一个在第一帧中带有按钮的影片剪辑。

  7. 选择该影片剪辑,然后在属性检查器中输入 balance_mc 作为实例名称。
  8. 在"动作"面板中输入下面的代码:
    balance_mc.top = balance_mc._y;
    balance_mc.bottom = balance_mc._y;
    balance_mc.left = balance_mc._x;
    balance_mc.right = balance_mc._x + 100;
    balance_mc._x += 50;
    balance_mc.handle_btn.onPress = function() {
        startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
    };
    balance_mc.handle_btn.onRelease = function() {
        stopDrag();
        var level:Number = Math.ceil((this._parent._x - this._parent.left - 50) * 2);
        this._parent._parent.song_sound.setPan(level);
    };
    balance_mc.handle_btn.onReleaseOutside = balance_mc.handle_btn.onRelease;
    

    startDrag() 参数 lefttoprightbottom 是在影片剪辑动作中设置的变量。

  9. 选择"控制">"测试影片"来使用平衡滑块。

有关 Sound 类的方法的更多信息,请参见"ActionScript 2.0 语言参考"中的 Sound。