3

我正在构建一个视频播放器,有点卡在音量滑块部分。这是一个 YouTube 风格的垂直滑块,这意味着如果滑块在顶部位置,音量应该是 100%,如果滑块被拖到底部位置,声音应该是 0。目前它正在做与我想要的相反的事情 :(

向下拖动滑块将使声音更大,而向上拖动则降低声音。

这是我下面处理音量滑块的代码。

// Sound Controller Settings ······························
   soundController = new SoundController();
   soundContrColor = soundController.colorChip;
   soundContrGray  = soundController.grayCover;
   soundContrGray.visible     = false;
   soundController.visible    = true;
   soundController.buttonMode = true;
   soundController.soundSlider.addEventListener(MouseEvent.MOUSE_DOWN, sliderDown);

// SoundController Button Mouse Events ························
   public function sliderDown(event:MouseEvent):void
   {
       soundController.soundSlider.startDrag(false, dragBounds);
       soundController.soundSlider.addEventListener(MouseEvent.MOUSE_MOVE, sliderMove);
       soundController.soundSlider.addEventListener(MouseEvent.MOUSE_UP, sliderUp);
       soundContrGray.visible = true;
   }

   public function sliderMove(event:MouseEvent):void
       {
       soundContrGray.height = soundController.soundSlider.y;
       userVolume = Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
       //userVolume = soundContrGray.height;
       setVolume(userVolume);

       trace("soundController.mouseY = "+soundController.soundSlider.y);
       trace("soundContrColor.height = "+Math.round(soundContrGray.height));
       trace("userVolume             = "+userVolume+"\r");

       event.updateAfterEvent();
       }

    public function sliderUp(event:MouseEvent):void
    {
        lastVolPoint = soundContrGray.height;
        setVolume(userVolume);
        event.updateAfterEvent();

        soundController.soundSlider.stopDrag();
        soundController.soundSlider.removeEventListener(MouseEvent.MOUSE_MOVE, sliderMove);
        soundController.soundSlider.removeEventListener(MouseEvent.MOUSE_UP, sliderUp);
    }

[TRACES] 当我一直拖到顶部时:

soundController.mouseY = 6
soundContrGray.height  = 6
userVolume             = 0

[TRACES] 当我一直向下拖动时:

soundController.mouseY = 56
soundContrGray.height  = 56
userVolume             = 30

我相信这就是问题所在:

userVolume = Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);

( -4)是一个偏移值,所以当你一直拖动它以将其关闭时,它是 0 而不是 4。
我需要以某种方式反转它,所以上面的痕迹将交换......下降将使 userVolume = 4和上升将使它成为30。

提前感谢任何看这个的人!:)

4

4 回答 4

4

我一定是错过了什么,因为它不可能这么简单,不是吗?

userVolume = 30-Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
于 2010-02-01T20:44:59.663 回答
2

因为当你想要 100% 音量时你得到 0,当你想要 0 音量时得到 30......试试这个

setVolume(100 - (userVolume * 100 / 30));

Substitude 0 in equation:
setVolume(100 - (0 * 100 / 30)) --> simplifies to
setVolume(100);

Substitute 30 in equation:
setVolume(100 - (30 * 100 / 30)); --> simplifies to
setVolume(100 - (3000 / 30)); --> simplifies to
setVolume(100 - 100); --> simplifies to
setVolume(0);
于 2010-02-01T20:40:40.703 回答
2

从概念上讲,您要做的是让声音越低越高soundContrGray。所以你需要一个最大值(或者在这种情况下是一个最大高度)来与当前值进行比较。我认为这30是音量滑块背景的高度。我将bg用于背景高度。

userVolume = (soundContrGray.height / bg.heigth);
userVolume = Math.round((1 - userVolume) * 100);

这将提供与实际元素的高度或它们在屏幕上的位置无关的相对音量设置。

于 2010-02-01T20:55:08.263 回答
1

userVolume = Math.round(100 * (1 - soundContrGray.height /(soundContrGray.y - 4)))

于 2010-02-01T20:58:10.480 回答