• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How I could play sounds when a button is being pressed

New Here ,
May 23, 2023 May 23, 2023

Copy link to clipboard

Copied

In this particular scene, I have 4 different buttons with 4 different narrations, and I want the selected button (which is any of the 4 buttons) to stop any narration that is currently playing, and then play the selected audio that is assigned to the button that is being pressed. I currently have no code so far regarding this issue, only coded the timeline navigation portion. I’ve only tried the “click to play/stop sound” but that hasn’t been doing it for me.346114218_155489270695198_3970223047196444813_n.png

 

348382312_971790250616286_9157094061490136888_n.png

 

TOPICS
ActionScript , How to

Views

72

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

Copy link to clipboard

Copied

LATEST

Hi.

 

You could write something like this for the sounds:

import flash.events.MouseEvent;
import flash.media.SoundChannel;

var channels:Object = {};
var narrationKey:String = "narration";

function stopAndPlaySound(sound:*, channel:String):void
{
	stopSound(channel);
	playSound(sound, channel);
}

function playSound(sound:*, channel:String):void
{	
	if (!channels[channel])
		channels[channel] = new SoundChannel();

	channels[channel] = sound.play();
}

function stopSound(channel:String):void
{
	if (channels[channel])
		channels[channel].stop();
}

// Narration0, Narration1, and so on are linkage names for the sounds in the Library
yourButton0.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{ stopAndPlaySound(new Narration0(), narrationKey); });
yourButton1.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{ stopAndPlaySound(new Narration1(), narrationKey); });
yourButton2.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{ stopAndPlaySound(new Narration2(), narrationKey); });
yourButton3.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{ stopAndPlaySound(new Narration3(), narrationKey); });

 

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines