我正在构建一个视频播放器,并且有点卡在音量滑块部分。这是一个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。
提前感谢任何看过这个的人! :)
答案 0 :(得分:4)
我必须遗漏一些东西,因为它不能这么简单,可以吗?
userVolume = 30-Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
答案 1 :(得分: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);
答案 2 :(得分:2)
从概念上讲,您要做的是让声音降低更高soundContrGray
。因此,您需要一个最大值(或在这种情况下为最大高度)来与当前值进行比较。我认为30
是音量滑块背景的高度。我会使用bg
作为背景高度。
userVolume = (soundContrGray.height / bg.heigth);
userVolume = Math.round((1 - userVolume) * 100);
这将给出一个相对音量设置,与实际元素的高度无关,或者它们在屏幕上的位置。
答案 3 :(得分:1)
userVolume = Math.round(100 * (1 - soundContrGray.height /(soundContrGray.y - 4)))