Copy link to clipboard
Copied
Hi All,
Forgive me if this is addressed elsewhere and kindly point me in the right direction. I am an infant with flash and action script so if you’re in doubt, error on the side of more specificity in your reply there's a lot of vocab I still don't know.
The Goal:
I have 4 audio files which are Soprano, Alto, Tenor, and Bass voices of one musical example. The result should allow the user to play all 4 voices at once and mute specific voices to hear all possible combinations of voices playing together or solo. I imagined a check box at the beginning of each notated musical line that mutes that voice when checked.
It would be ideal to have Play, Pause, and Stop playback options where ‘Stop’ resets playback to the beginning and ‘Pause’ maintains the current place in time and resumes from that point once playback is initiated again.
I am able to get single sound files to play from one button, but I’m struggling to find how to link multiple audio files to start at the same time from one button. I found this link: https://forums.adobe.com/thread/1069107?start=0&tstart=0
where the last post mentions using arrays, but being a complete beginner I don’t completely understand what they do.
I know it’s a big chunk of questions all at once but if you could address any portion of it, or direct me to a tutorial/instructions, I’d really appreciate it.
Thank you for taking the time to help others
use:
...
kglad wrote:
use:
import flash.events.MouseEvent;
import flash.media.SoundTransform;
var soprano: Sound = new Soprano();
var alto: Sound = new Alto();
var tenor: Sound = new Tenor();
var bass: Sound = new Bass();
var soundA: Array = [soprano, alto, tenor, bass];
var offsetA:Array = []
var st: SoundTransform = new SoundTransform();;
play_btn.addEventListener(MouseEvent.CLICK, playF);
pause_btn.addEventListener(MouseEvent.CLICK, pauseF);
stop_btn.addEventListener(MouseEvent.CLICK, stop
Copy link to clipboard
Copied
assign your 4 library sounds linkage ids (aka class names, eg Soprano,Alto,Tenor,Bass) and use something like:
var soprano:Sound=new Soprano();
var alto:Sound=new Alto();
var tenor:Sound=new Tenor();
var bass:Sound=new Bass();
var soundA:Array=[soprano,alto,tenor,bass];
var stA:Array = [];
you can now use all the sound methods and properties and apply them to soprano, alto, tenor and bass:
play_btn.addEventListener(MouseEvent.CLICK,playF);
pause_btn.addEventListener(MouseEvent.CLICK,pauseF);
stop_btn.addEventListener(MouseEvent.CLICK,stopF);
function playF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
stA.length=0;
stA.push(soundA.play());
}
}
function pauseF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
soundA.pause();
}
}
function playF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
soundA.stop();
}
}
to mute the various voices, assign their volume to 0. eg, for alto which has array index 1
st=stA[1].soundTransform;
st.volume=0;
stA[1].soundTransform=st;
to unmute bass:
st=stA[3].soundTransform;
st.volume=1;
stA[3].soundTransform=st;
Copy link to clipboard
Copied
Thank you for your reply kglad. I look forward to making my way through it and trying it out. But again, being new to flash and action script, this is a bit challenging because I lack fundamental vocabulary and understanding. From what I've read, to assign linkage ids I should be able to right click on the mp3 from the library window and choose "linkage" But I have no such option (cs6). Am I incorrect? Is there a different way to assign linkage IDs? I sincerely thank you for your patience.
Copy link to clipboard
Copied
either double click adjacent to your library sound in the as linkage column, or right click>click properties>tick export for actionscript>assign a class.
Copy link to clipboard
Copied
Again, thank you. Just a few more rookie questions:
1. I assume I should have each sound (soprano, alto, tenor, bass) on it's own Layer, correct?
2. I'm trying to distinguish which values in your code are placeholders (if any) and which should stay as they are since I'm far from fluent with Actionscript 3.
Specifically these instances:
-the 'F' in: ...MouseEventCLICKplayF ...pauseF etc.
&
-the 'soundA' in soundAlength
3. In your example of "alto which has array index of 1.." (and the following bass example which has index of 3?) Where or how do I specify which voice is assigned which index number? Or is the index number automatically generated by the order in which they were listed at the top.
For example:
var soprano:Sound=new Soprano();
is this array index of 0 since it's first in the list, Alto 1, tenor, 3 and Bass 4, or do I actually have to enter the values in the parentheses and refer to those same number values, whatever they are, as the array index values throughout the code?
Copy link to clipboard
Copied
no sound will be attached to a timeline/frame/layer.
nothing in my code is a placeholder if you assign your linkage ids/classes like i suggested (Alto,etc)
Copy link to clipboard
Copied
Ok I think I'm catching on. But first, should your third function entry start out "function stopF..." instead of "function playF...." ?
I added 4 mute buttons from the common libraries and called them mute_s, mute_a, mute_t, and mute_b.
Am I correct to add the following below your code "stop_btn addEventLis....":
mute_s.addEventListener(MouseEvent.CLICK,muteS);
mute_a.addEventListener(MouseEvent.CLICK,muteA);
mute_t.addEventListener(MouseEvent.CLICK,muteT);
mute_b.addEventListener(MouseEvent.CLICK,muteB);
And then to mute:
function muteS(e:MouseEvent):void{
st=stA[0].soundTransform;
st.volume=0;
stA[0].soundTransform=st;
}
function muteA(e:MouseEvent):void{
st=stA[1].soundTransform;
st.volume=0;
stA[1].soundTransform=st;
}
function muteT(e:MouseEvent):void{
st=stA[2].soundTransform;
st.volume=0;
stA[2].soundTransform=st;
}
function muteB(e:MouseEvent):void{
st=stA[3].soundTransform;
st.volume=0;
stA[3].soundTransform=st;
}
And to unmute:
function muteS(e:MouseEvent):void{
st=stA[0].soundTransform;
st.volume=1;
stA[0].soundTransform=st;
}
etc. ??
Copy link to clipboard
Copied
yes, below pauseF should be stopF
function stopF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
soundA.stop();
}
}
and your mute code looks ok.
Copy link to clipboard
Copied
Thank you kglad.
I wasn't sure if the mute and unmute script could apply to/reference the same button so I made have 4 mute buttons (mute_S, mute_A, etc) and 4 play/unmute buttons (unS, unA, etc).
I have the following compiler errors that each point to my first line of code in each of the 4 unmute instances. Do you know what I should do differently? Should scrap having two separate buttons for mute and unmute? I used the classic arcade buttons. I wasn't sure if they could toggle the mute function or only handle one function.
Scene 1, Layer 'scripts', Frame 1, Line 65 | 1023: Incompatible override. |
Scene 1, Layer 'scripts', Frame 1, Line 65 | 1021: Duplicate function definition. |
function unS(e:MouseEvent):void{
st=stA[0].soundTransform;
st.volume=1;
stA[0].soundTransform=st;
}
function unA(e:MouseEvent):void{
st=stA[1].soundTransform;
st.volume=1;
stA[1].soundTransform=st;
}
function unT(e:MouseEvent):void{
st=stA[2].soundTransform;
st.volume=1;
stA[2].soundTransform=st;
}
function unB(e:MouseEvent):void{
st=stA[3].soundTransform;
st.volume=1;
stA[3].soundTransform=st;
}
Copy link to clipboard
Copied
line 65 in frame 1 is defining a function that's already been defined.
Copy link to clipboard
Copied
Now I have a compiler errors
"Access of undefined property st." for every line that contains st from the mute code down.
"Access of undefined property unmuteS, unmuteA, unmuteT, & unmuteB
How do I properly define these?
import flash.events.MouseEvent;
var soprano:Sound=new Soprano();
var alto:Sound=new Alto();
var tenor:Sound=new Tenor();
var bass:Sound=new Bass();
var soundA:Array=[soprano,alto,tenor,bass];
var stA:Array = [];
play_btn.addEventListener(MouseEvent.CLICK,playF);
pause_btn.addEventListener(MouseEvent.CLICK,pauseF);
stop_btn.addEventListener(MouseEvent.CLICK,stopF);
mute_s.addEventListener(MouseEvent.CLICK,muteS);
mute_a.addEventListener(MouseEvent.CLICK,muteA);
mute_t.addEventListener(MouseEvent.CLICK,muteT);
mute_b.addEventListener(MouseEvent.CLICK,muteB);
unmuteS.addEventListener(MouseEvent.CLICK,mute);
unmuteA.addEventListener(MouseEvent.CLICK,unA);
unmuteT.addEventListener(MouseEvent.CLICK,unT);
unmuteB.addEventListener(MouseEvent.CLICK,unB);
//Mute code
function muteS(e:MouseEvent):void{
st=stA[0].soundTransform;
st.volume=0;
stA[0].soundTransform=st;
}
function muteA(e:MouseEvent):void{
st=stA[1].soundTransform;
st.volume=0;
stA[1].soundTransform=st;
}
//etc
//unmute
{
st=stA[0].soundTransform;
st.volume=1;
stA[0].soundTransform=st;
}
{
st=stA[1].soundTransform;
st.volume=1;
stA[1].soundTransform=st;
}
//etc
Copy link to clipboard
Copied
Correction**
unmuteS.addEventListener(MouseEvent.CLICK,unS);
unmuteA.addEventListener(MouseEvent.CLICK,unA);
//etc
Copy link to clipboard
Copied
var st:SoundTransform;
Copy link to clipboard
Copied
In attempting to simplify things I saved another file removing all the mute buttons to test the play, pause, and stop functions. Play works. Clicking Stop and Pause results in:
TypeEror: Error #1006: pause is not a function.
at AEhelptoggle_fla::MainTimeline/pauseF()
TypeError: Error #1006: stop is not a function.
at AEhelptoggle_fla::MainTimeline/stopF()
Below is the code:
import flash.events.MouseEvent;
var soprano:Sound=new Soprano();
var alto:Sound=new Alto();
var tenor:Sound=new Tenor();
var bass:Sound=new Bass();
var soundA:Array=[soprano,alto,tenor,bass];
var stA:Array = [];
var st:SoundTransform;
play_btn.addEventListener(MouseEvent.CLICK,playF);
pause_btn.addEventListener(MouseEvent.CLICK,pauseF);
stop_btn.addEventListener(MouseEvent.CLICK,stopF);
function playF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
stA.length=0;
stA.push(soundA.play());
}
}
function pauseF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
soundA.pause();
}
}
function stopF(e:MouseEvent):void{
for(var i:int=0;i<soundA.length;i++){
soundA.stop();
}
}
Copy link to clipboard
Copied
use:
import flash.events.MouseEvent;
import flash.media.SoundTransform;
var soprano: Sound = new Soprano();
var alto: Sound = new Alto();
var tenor: Sound = new Tenor();
var bass: Sound = new Bass();
var soundA: Array = [soprano, alto, tenor, bass];
var scA: Array = [];
var offsetA:Array = []
var st: SoundTransform = new SoundTransform();;
play_btn.addEventListener(MouseEvent.CLICK, playF);
pause_btn.addEventListener(MouseEvent.CLICK, pauseF);
stop_btn.addEventListener(MouseEvent.CLICK, stopF);
function playF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
scA.length = 0;
scA.push(soundA.play());
}
}
function pauseF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = scA.position;
scA.stop();
}
}
function stopF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = 0;
scA.stop();
}
}
function unpauseF(e:MouseEvent):void{
for(var i: int = 0; i < soundA.length; i++) {
scA = soundA.play(offsetA);
}
}
function muteF(i:int):void{
st.volume = 0;
scA.soundTransform = st;
}
function unmuteF(i:int):void{
st.volume = 1;
scA.soundTransform = st;
}
Copy link to clipboard
Copied
With your most recent code, "play" works. However, both Pause and Stop only effect the Bass voice. Clicking "pause" again does not bring the bass voice back in.
I receive the following errors when clicking Pause and Stop:
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/stopF()
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/pauseF()
Copy link to clipboard
Copied
replace scA everywhere by this['sc_'+i]
Copy link to clipboard
Copied
All of them except the first instance returned these 2 compiler errors:
Scene 1, Layer 'scripts', Frame 1, Line 48 | 1084: Syntax error: expecting identifier before dot. |
Scene 1, Layer 'scripts', Frame 1, Line 48 | 1064: Invalid metadata. |
The code for reference where scA started:
function pauseF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = ['sc_'+i].position;
['sc_'+i].stop();
}
}
function stopF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = 0;
['sc_'+i].stop();
}
}
function unpauseF(e:MouseEvent):void{
for(var i: int = 0; i < soundA.length; i++) {
['sc_'+i] = soundA.play(offsetA);
}
}
function muteF(i:int):void{
st.volume = 0;
['sc_'+i].soundTransform = st;
}
function unmuteF(i:int):void{
st.volume = 1;
['sc_'+i].soundTransform = st;
Copy link to clipboard
Copied
'this' is a keyword.
kglad wrote:
replace scA everywhere by this['sc_'+i]
Copy link to clipboard
Copied
Play still works. Pause and Stop now yield these errors:
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/stopF()
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/pauseF()
Code:
import flash.events.MouseEvent;
import flash.media.SoundTransform;
var soprano: Sound = new Soprano();
var alto: Sound = new Alto();
var tenor: Sound = new Tenor();
var bass: Sound = new Bass();
var soundA: Array = [soprano, alto, tenor, bass];
var scA: Array = [];
var offsetA:Array = []
var st: SoundTransform = new SoundTransform();;
play_btn.addEventListener(MouseEvent.CLICK, playF);
pause_btn.addEventListener(MouseEvent.CLICK, pauseF);
stop_btn.addEventListener(MouseEvent.CLICK, stopF);
function playF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
scA.length = 0;
scA.push(soundA.play());
}
}
function pauseF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = this['sc_'+i].position;
this['sc_'+i].stop();
}
}
function stopF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = 0;
this['sc_'+i].stop();
}
}
function unpauseF(e:MouseEvent):void{
for(var i: int = 0; i < soundA.length; i++) {
this['sc_'+i] = soundA.play(offsetA);
}
}
function muteF(i:int):void{
st.volume = 0;
this['sc_'+i].soundTransform = st;
}
function unmuteF(i:int):void{
st.volume = 1;
this['sc_'+i].soundTransform = st;
}
Copy link to clipboard
Copied
again
replace every single/each and every scA everywhere by this['sc_'+i]
Copy link to clipboard
Copied
Does scA refer to or represent a group of identifiers with other text that I should be looking for?
Sorry if this is obvious, but I'm assuming: scA
is different from: scA
I have no instances of: scA
In the last batch of code.
I tried replacing the remaining three instances of scA with this['sc_'+i]
and I get the following compiler errors:
Scene 1, Layer 'scripts', Frame 1, Line 10 | 1084: Syntax error: expecting identifier before this. |
Scene 1, Layer 'scripts', Frame 1, Line 10 | 1086: Syntax error: expecting semicolon before rightbracket. |
Scene 1, Layer 'scripts', Frame 1, Line 48 | 1084: Syntax error: expecting identifier before dot. |
Scene 1, Layer 'scripts', Frame 1, Line 48 | 1064: Invalid metadata. |
Copy link to clipboard
Copied
use:
import flash.events.MouseEvent;
import flash.media.SoundTransform;
var soprano: Sound = new Soprano();
var alto: Sound = new Alto();
var tenor: Sound = new Tenor();
var bass: Sound = new Bass();
var soundA: Array = [soprano, alto, tenor, bass];
var offsetA:Array = []
var st: SoundTransform = new SoundTransform();;
play_btn.addEventListener(MouseEvent.CLICK, playF);
pause_btn.addEventListener(MouseEvent.CLICK, pauseF);
stop_btn.addEventListener(MouseEvent.CLICK, stopF);
function playF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
this['sc_'] = soundA.play());
}
}
function pauseF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = this['sc_'+i].position;
this['sc_'+i].stop();
}
}
function stopF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = 0;
this['sc_'+i].stop();
}
}
function unpauseF(e:MouseEvent):void{
for(var i: int = 0; i < soundA.length; i++) {
this['sc_'+i] = soundA.play(offsetA);
}
}
function muteF(i:int):void{
st.volume = 0;
this['sc_'+i].soundTransform = st;
}
function unmuteF(i:int):void{
st.volume = 1;
this['sc_'+i].soundTransform = st;
}
Copy link to clipboard
Copied
I removed one: " ) "
from the end of line 19 after a compiler error. Then when testing play works but the Pause and Stop buttons yield the following errors:
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/stopF()
TypeError: Error #1010: A term is undefined and has no properties.
at AEhelp2_fla::MainTimeline/pauseF()
In the properties tabs for those buttons, the first field shows the correct names fro both: pause_btn and stop_btn
Copy link to clipboard
Copied
use:
kglad wrote:
use:
import flash.events.MouseEvent;
import flash.media.SoundTransform;
var soprano: Sound = new Soprano();
var alto: Sound = new Alto();
var tenor: Sound = new Tenor();
var bass: Sound = new Bass();
var soundA: Array = [soprano, alto, tenor, bass];
var offsetA:Array = []
var st: SoundTransform = new SoundTransform();;
play_btn.addEventListener(MouseEvent.CLICK, playF);
pause_btn.addEventListener(MouseEvent.CLICK, pauseF);
stop_btn.addEventListener(MouseEvent.CLICK, stopF);
function playF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
this['sc_'+i] = soundA.play();
}
}
function pauseF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = this['sc_'+i].position;
this['sc_'+i].stop();
}
}
function stopF(e: MouseEvent): void {
for(var i: int = 0; i < soundA.length; i++) {
offsetA = 0;
this['sc_'+i].stop();
}
}
function unpauseF(e:MouseEvent):void{
for(var i: int = 0; i < soundA.length; i++) {
this['sc_'+i] = soundA.play(offsetA);
}
}
function muteF(i:int):void{
st.volume = 0;
this['sc_'+i].soundTransform = st;
}
function unmuteF(i:int):void{
st.volume = 1;
this['sc_'+i].soundTransform = st;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now