Skip to main content
BigNerd
Known Participant
May 27, 2015
Answered

How to play multiple audio files at the same time and toggle mute certain voices?

  • May 27, 2015
  • 1 reply
  • 2115 views

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

This topic has been closed for replies.
Correct answer kglad

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;

}


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;

}

1 reply

kglad
Community Expert
Community Expert
May 27, 2015

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;

BigNerd
BigNerdAuthor
Known Participant
May 27, 2015

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. 

kglad
Community Expert
Community Expert
June 1, 2015

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


var st:SoundTransform;