Copy link to clipboard
Copied
Hello,
Currently I am having an issue figuring out how to get one button to control my audio. As a work-around, I'm using a slider, but I would love to have a single button controlling the volume of my audio. For a little more information, I am trying to get a single button to change the volume from 100%, to 50%, then to 25%, then to 0%, and back to 100%, while also changing the graphic used for each volume state. Any help would be greatly appreciated! Thanks!
1 Correct answer
the same:
var sc:SoundChannel = strife.play();
var voltransform:SoundTransform = new SoundTransform();
var volA:Array = [.50,.25,0,1.00];
var index:int = 0;
your_button.addEventListener(MouseEvent.CLICK, clickF);
function clickF(e:MouseEvent):void{
setVolume(volA[index]);
index=(index+1)%volA.length;
index=(index+1)%volA.length;
setGraphicF(index+1);
}
function setVolume(vol){
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
function setGraphic(n:int):void{
your_button.gotoAndStop(n
...Copy link to clipboard
Copied
what code are you using that's not working?
Copy link to clipboard
Copied
Hi!
Right now I'm using this code:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import fl.events.SliderEvent;
Slider.visible = false;
Max_btn.Mute_btn.visible = false;
var s:Sound = new Sound();
var strife:Strife = new Strife();
var sc:SoundChannel = strife.play();
function setVolume(vol){
var volTransform:SoundTransform = new SoundTransform();
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
var voltransform:SoundTransform = new SoundTransform();
Slider.addEventListener(SliderEvent.THUMB_DRAG,changevol);
function changevol(event:SliderEvent):void{
voltransform.volume = Slider.value;
SoundMixer.soundTransform = voltransform
}
var Slider_control:Boolean = false;
Max_btn.addEventListener(MouseEvent.CLICK, toggleSlider_control);
function toggleSlider_control(event:MouseEvent){if(Slider_control == true){Slider_control = false;
Slider.visible = false;
}
else{Slider_control = true;
Slider.visible = true;
}
}
That's not what I want to use though, I'd like to have it be a single button that doesn't bring up a slider, and instead changes the volume multiple levels. I've tried using an "if else..." statement, but that only takes the first bit I give it, and won't go back. I can make it a toggle button for "on off", but that isn't what I want either.
Copy link to clipboard
Copied
var sc:SoundChannel = strife.play();
var voltransform:SoundTransform = new SoundTransform();
var volA:Array = [50,25,0,100];
var index:int = 0;
your_button.addEventListener(MouseEvent.CLICK, clickF);
function clickF(e:MouseEvent):void{
setVolume(volA[index]);
index=(index+1)%volA.length;
}
function setVolume(vol){
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
Copy link to clipboard
Copied
Inserting that code gives me an "Access of undefined property volTransform" error.
Copy link to clipboard
Copied
use voltransform
Copy link to clipboard
Copied
I've gotten the code to play nice with my existing code, removed the slider portion of the code, so that works now. Now my audio starts normally, but when I click the button, it cycles between ear-rape, slightly less ear-rape, and mute. Is there also a way to change the graphic to correspond to the volume level?
Copy link to clipboard
Copied
oops, that's my fault. use:
var volA:Array = [.5,.25,0,1.];
and yes, you can change a graphic. you just have to decide how you want to do that. eg, change movieclip frames, use different images,something else?
Copy link to clipboard
Copied
Nice! That worked wonders! Thank you so much!
For my previous project, I used a button that had a mute symbol inside a movieclip, and I hid and revealed that part of it. Not the most ideal way of doing it, but how would I go about changing the graphic so that previous graphics hide, while keeping it clickable?
Copy link to clipboard
Copied
any shape with an alpha of 0 is clickable and not visible.
but you probably want to display 4 graphics on 4 different keyframes with that approach. the easiest way to set that up is to put the 100% volume on frame 1, the 50% volume on frame 2, the 25% on frame 3 and the mute on frame 4. put a stop() on frame 1.
you could then use:
function clickF(e:MouseEvent):void{
setVolume(volA[index]);
index=(index+1)%volA.length;
setGraphicF(index+1);
}
function setGraphic(n:int):void{
your graphicmovieclip.gotoAndStop(n);
}
Copy link to clipboard
Copied
Looking at my question, I think I was confusing myself.
How would I go about making a single movie clip (to function as the button) and change graphics in the movie clip?
Copy link to clipboard
Copied
the same:
var sc:SoundChannel = strife.play();
var voltransform:SoundTransform = new SoundTransform();
var volA:Array = [.50,.25,0,1.00];
var index:int = 0;
your_button.addEventListener(MouseEvent.CLICK, clickF);
function clickF(e:MouseEvent):void{
setVolume(volA[index]);
index=(index+1)%volA.length;
index=(index+1)%volA.length;
setGraphicF(index+1);
}
function setVolume(vol){
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
function setGraphic(n:int):void{
your_button.gotoAndStop(n);
}
Copy link to clipboard
Copied
I'm getting a "TypeError: Error #1010: A term is undefined and has no properties. at Test_Project_fla::MainTimeline/frame1()". And when I test the animation, it doesn't progress past the first frame, but my volume button switches between all of it's frames in a loop.
Copy link to clipboard
Copied
oops, that volTransform needs to be voltransform again.
Copy link to clipboard
Copied
It still seems to be doing that. :T
Edit: It seems I made a goof, and the way I made my movie clip was weird.
Edit 2: It works! Thank you so much!
Copy link to clipboard
Copied
you're welcome.

