Dart:Scalable Application Development
上QQ阅读APP看书,第一时间看更新

Playing sound in the browser

This feature was deliberately left until last as continually listening to the same sound effect can become quite irritating! MP3 is probably the best supported format for various web browsers, with one exception—Dartium.

Tip

Dartium is built from the Chromium open source project. The project does not include any proprietary codecs including MP3. When creating projects, it is sometimes fine to just use MP3 and test the audio parts of the application in another browser.

For this project, three formats of the same sound file are provided. The OGG format will run from Dartium and most browsers. If you are having trouble, try another format.

Producing sound effects

To create a sound effect to play when the slide changes, a tool such as Bfxr can be used. It can be found on the Web at http://www.bfxr.net/ (where there are links to downloads for standalone versions as well) and is a powerful audio creation tool.

As it was created for use during game jams (time-constrained game coding contests), it is easy to pick up and fast to use. Let's have a look at the following screenshot:

Producing sound effects

Creating sound files

Audacity (http://web.audacityteam.org/) is a popular and stable audio editing and recording package. It is an open source desktop application and is available for all the major OS platforms. Let's have a look at the following screenshot:

Creating sound files

You can perform some last minute tweaks to your sound and export it (File | Export..) in a variety of formats (such as, MP3, Ogg, and WAV). On some systems, you may need to download an MP3 codec.

Note

Keep an eye on the file size and duration time, though! Ideally, the sound should be around 200ms long.

Loading sounds

The sound is loaded in a method that is called from the constructor of the SlideShowApp object, and as you expect with Dart, this in an asynchronous operation:

  List loadAudio() {
    slideChange = new AudioElement("snd/slidechange.ogg");
    slideChange.preload = "auto";
    return [slideChange.onLoad.first];
  }

  playSnd() {
    slideChange
      ..play()
      ..onEnded.listen(done);
  }

  done(e) {
    slideChange.load();
  }

The onLoad property of AudioElement is a stream of load events for this object (it is possible for audio and other media elements to load more than one file).

The first property of this stream waits for the first load event and then stops listening to the stream. This is a useful way to ensure that all media, such as images and sounds for a game, are loaded before they are used.

Playing back sounds

Once all that set up is done, actually playing the audio is merely a case of calling the playSnd method and ensuring that the speakers are plugged in! The onEnded event is listened to and a callback is provided. This deals with a browser quirk that requires the file to be reloaded before it can be replayed.

That quirk serves to remind us that although audio support in browsers has greatly improved, it is geared toward large media playback, and playing short sound effects can be a little more work than it should be.

Note

Here's a website that you can use to help contribute to Dart development with either bugs or feature suggestions:

http://dartbug.com

This redirects (at time of writing) you to a Google Code project. The Google Code service is being wound down but will be kept going for some time for the Chromium project. As the Dart team is part of the Chrome team at Google, it is possible that parts of the project will live on both Google Code and GitHub.

If you have an idea or think you have found a bug, it is a good idea to search the existing issues or discuss it on Google+ (http://g.co/dartisans) or a mailing list before raising that issue.