# Providing a control to turn off sounds that play automatically in Flash

ID: FLASH18

Technology: flash

Type: Technique

## When to Use

- Adobe Flash Professional version MX and higher
- Adobe Flex

## Description

The intent of this technique is to allow a user to turn off sounds that start automatically when a Flash movie loads. The control to turn off the sounds should be located near the beginning of the page to allow the control to be easily and quickly discovered by users . This is useful for those who utilize assistive technologies (such as screen readers, screen magnifiers, switch mechanisms, etc.) and those who may not (such as those with cognitive, learning and language disabilities).

In this technique, an author includes a control that makes it possible for users to turn off any sounds that are played automatically. For maximum accessibility, the control can be added to the HTML document rather than to the Flash movie. The HTML control will communicate with the Flash movie through the ExternalInterface class. This means that the user can control the sound playback without having to interact with Flash content. If this is not practical, the control can be provided within the Flash content, provided that the control is keyboard operable, located early in the tab and reading order, and clearly labeled to indicate that it will turn off the sounds that are playing.

## Examples

### Providing a button in the Flash to stop sound

This example demonstrates the addition of a button within the Flash movie to allow the user to stop sounds from playing. A class called SoundHandler is created which automatically starts playing an mp3 file when the movie loads.

    package wcagSamples {
      import flash.display.Sprite;
      import flash.net.URLRequest;
      import flash.media.Sound;
      import flash.media.SoundChannel;

      import fl.controls.Button;
      import fl.accessibility.ButtonAccImpl;

      import flash.events.MouseEvent;
      public class SoundHandler extends Sprite {
        private var snd: Sound = new Sound();
        private var button: Button = new Button();
        private var req: URLRequest = new URLRequest("http://av.adobe.com/podcast\
          /csbu_dev_podcast_epi_2.mp3");
        private var channel: SoundChannel = new SoundChannel();

        public function SoundHandler() {
          ButtonAccImpl.enableAccessibility();
          button.label = "Stop Sound";
          button.x = 10;
          button.y = 10;
          button.addEventListener(MouseEvent.CLICK, clickHandler);
          this.addChild(button);
          snd.load(req);
          channel = snd.play();
        }
        private function clickHandler(e: MouseEvent): void {
          if (button.label == "Stop Sound") {
            button.label = "Start Sound";
            channel.stop();
          } else {
            channel = snd.play();
            button.label = "Stop Sound";
          }
        }
      }
    }

This is demonstrated in the working example of Providing a button in the Flash to stop sound. The source of Providing a button in the Flash to stop sound is available.

### Providing a button in the HTML before the Flash object to stop sound

A class called SoundHandler is created which automatically starts playing an mp3 file when the movie loads. An HTML button is placed in the HTML document containing the Flash movie. When the button is clicked the action is communicated between the HTML page and the Flash movie via the Flash Player JavaScript API, resulting in the toggleSound method being called on the SoundHandler class.

ActionScript 3.0 code for Example 2

    package wcagSamples {
      import flash.display.Sprite;
      import flash.external.ExternalInterface;
      import flash.net.URLRequest;
      import flash.media.Sound;
      import flash.media.SoundChannel;

      import flash.events.MouseEvent;
      public class SoundHandler extends Sprite {
        private var snd: Sound = new Sound();
        private var soundOn: Boolean = true;
        private var req: URLRequest = new URLRequest("http://av.adobe.com/podcast/\
          csbu_dev_podcast_epi_2.mp3");
        private var channel: SoundChannel = new SoundChannel();

        public function SoundHandler() {
          if (ExternalInterface.available)
          ExternalInterface.addCallback("toggleSound", this.toggleSound);
          snd.load(req);
          channel = snd.play();
        }

        private function toggleSound(enable: Boolean): void {
          if (! enable) {
            channel.stop();
            soundOn = true;
          } else {
            channel = snd.play();
            soundOn = true
          }
        }
      }
    }

HTML code for Example 2

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
        <title>Flash Sound Toggle example</title>
        <script src="swfobject.js" type="text/javascript"/>
        <script type="text/javascript">
        function $(id) {
            return document.getElementById(id);
        }

        swfobject.embedSWF("html_control_to_toggle_audio_as3.swf",
          "flashPlaceHolder", "0", "0", "8");
        function init() {
                var soundOn = true;
                $("soundToggle").onclick = function(event){
                    soundOn = !soundOn;
                    $("flashPlaceHolder").toggleSound(soundOn);
                    event.target.value = soundOn ? "Stop Sound" : "Start Sound";
                };
        }
        window.onload = init;
    </script>

      </head>
      <body id="header">
        <h1>Flash Automatic Sound Demo</h1>
        <p>This page contains a Flash movie that automatically starts
          playing sound. Use the button below to stop or start the
          sound</p>
        <input id="soundToggle" type="button" value="Stop Sound"/>
        <p id="flashPlaceHolder">Flash needs to be installed for this
          example to work</p>
      </body>
    </html>

This is demonstrated in the working example of Providing a button in the HTML before the Flash object to stop sound. The source of source of Providing a button in the HTML before the Flash object to stop sound is available.

## Tests

### Procedure

For Flash movies that automatically start playing sound after loading:

1. Confirm that an HTML control that conforms to WCAG 2.0 is placed at the beginning of the document's tab order
2. If there is no HTML-based control, confirm that an accessible control is placed at the beginning of the Flash movie's tab order.
3. Activate the HTML or Flash-based control
4. Verify that audio playback stops

### Expected Results

- Check #1 or #2 is true, and #4 is true.

## Related Techniques

- G170