actionscript 3 using sound related classes by martin stanhope the university of bolton

22
ActionScript 3 Using Sound Related Classes by Martin Stanhope The University of Bolton

Upload: gavin-logan

Post on 13-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

ActionScript 3

Using Sound Related Classesby

Martin StanhopeThe University of Bolton

Sound related AS3 classes

•‘Rich content’ web-applications frequently use sound to enhance the user experience.

•Flash CS3 ActionScript3 offers the web developer a collection of classes that are used to handle external sound files.

•Sound()

•SoundChannel()

•SoundEvent()

•SoundLoaderContext()

•SoundMixer()

•SoundTransform()

Details of the Sound class from the help pages

SoundApp1a.as - Playing an mp3 music file – Technique 1package {

import flash.display.Sprite; import flash.media.Sound; // The Sound classimport flash.net.URLRequest; // A class for accessing external filespublic class SoundApp1a extends Sprite {

public function SoundApp1a() {

var mp3File:String = “mytrack.mp3”;var request:URLRequest =new

URLRequest(mp3File);mySound = new Sound();mySound.load(request);mySound.play();

} // End of constructor

} // End of class} // End of package specification

SoundApp1b.as - Playing an mp3 music file – Technique 2package {

import flash.display.Sprite; import flash.media.Sound;import flash.net.URLRequest;public class SoundApp1b extends Sprite {

public function SoundApp1b() {

var mp3File:String = “mytrack.mp3”;mySound = new Sound();mySound.load( new URLRequest(mp3File) );mySound.play();

} // End of constructor

} // End of class} // End of package specification

SoundApp1c.as - Playing an mp3 music file – Technique 3package {

import flash.display.Sprite; import flash.media.Sound;import flash.net.URLRequest;public class SoundApp1c extends Sprite {

public function SoundApp1c() { var mp3File:String = “mytrack.mp3”; var request:URLRequest = new URLRequest(mp3File);

mySound = new Sound(request); //Pass request to constructor mySound.play(); // No call to load() when using this technique

} // End of constructor

} // End of class} // End of package specification

SoundApp1d.as - Playing an mp3 music file – Technique 4package {

import flash.display.Sprite; import flash.media.Sound;import flash.net.URLRequest;public class SoundApp1d extends Sprite {

public function SoundApp1d() {

var mp3File:String = “mytrack.mp3”;

mySound = new Sound( new URLRequest( mp3File ) ); mySound.play();

} // End of constructor

} // End of class} // End of package specification

SoundApp1e.as - Applying a play offset in millisecondspackage {

import flash.display.Sprite; import flash.media.Sound;import flash.net.URLRequest;public class SoundApp1e extends Sprite {

public function SoundApp1e() {

var mp3File:String = “myTrack.mp3";var request:URLRequest = new URLRequest(mp3File);mySound = new Sound();mySound.load(request);mySound.play(5000); // start play 5 seconds into track

} // End of constructor

} // End of class} // End of package specification

Creating a user selectable play button•This demonstration program allows the user to pick a button to play a test sound track.

•At the heart of this program is an event handler which is executed automatically when the mouse is clicked over the play button. The handler simply plays the sound track.

MP3 Sound File Tester Application

by

Joe Bloggs – April 2008

Play Test Track

Close the application to stop the track playing

Program structure – example 1

// The application class

// Private properties (data variables)

// The names of private properties are often prefixed with an underscore

// The public constructor has same name as the class

// All the coding for the application goes here but it will be // difficult to follow as it is not written in a modular fashion.

// The package statement

// Import statements to bring the required Flash library classes

Program structure – example 2

// The application class

// Import statements to bring the required Flash library classes

// Private properties (data variables)

// The names of private properties are often prefixed with an underscore

// The public constructor has same name as the class

// Contains calls to the private helper functions. This makes it more modular and easier to understand.

// A private helper function

// A private helper function

// The package statement

SoundApp2.as – Details of the application class coding

public class SoundApp2 extends Sprite {

//Private variables go here

private var _mySound:Sound; // A private variable to hold a sound object

public function SoundApp2() // The constructor {

displayTitle();displayPlayButton();displayInstructions();

}

// Private helper functions go here under the constructor

// The mouse click event handler function will go under here

}

Coding of the helper function: displayTitle()

private function displayTitle():void{

var titleTxt:TextField = new TextField();addChild(titleTxt);titleTxt.x = 50;

titleTxt.y = 20; titleTxt.width = 100; titleTxt.height = 20; titleTxt.border = true; // You may prefer false

titleTxt.text = “MP3 Sound File Tester";}

Coding of the helper function: displayPlayButton()private function displayPlayButton():void{

var playBtn:Sprite = new Sprite(); // Sprite container for graphicplayBtn.x = 80;

playBtn.y = 200; playBtn.graphics.lineStyle(1); playBtn.graphics.beginFill(0xBBBBBB); // A shade of gray playBtn.graphics.drawRoundRect(0, 0, 50, 20, 5, 5); playBtn.addEventListener( MouseEvent.CLICK, onMouseClick ); addChild(playBtn);

// Create the label to sit on the button var playLbl:TextField = new TextField(); playLbl.text = "Play"; playLbl.selectable = false; //Text not selectable

playLbl.width = 50;playLbl.height = 20;playBtn.addChild(playLbl); //Put text inside button container

}

Coding the the helper function: displayInstructions()

private function displayInstructions():void{

var instructionsTxt:TextField = new TextField();

addChild(instructionsTxt);instructionsTxt.x = 50;

instructionsTxt.y = 20; instructionsTxt.width = 100; instructionsTxt.height = 20; instructionsTxt.border = true; instructionsTxt.text =

“Close app to stop sound”;}

Coding of the mouse click event handler

private function onMouseClick( event:MouseEvent ):void{

var mp3File:String = “mytrack.mp3”; var request:URLRequest = new URLRequest(mp3File);

_mySound = new Sound(); _mySound.load(request); _mySound.play();

}

Creating a stop button• The Sound class contains a method named close()

that can be used to fully stop the sound track. Note that the method is not named stop().

• When close() is used the track cannot be restarted from the point it was stopped at.

• We will later see how a track can be paused and restarted from the pause point. This technique will use a class named SoundChannel which contains a method named stop().

Things to do to add a stop button

• In the application constructor SoundApp2, add a call to a private helper function named: displayStopButton();

• Under the SoundApp2 constructor, write the code of the displayStopButton() method, it will be similar to displayPlayButton() and will include a statement to register a mouse click event handler associated with the stop button.

• Write the event handler, which simply calls the method to stop the sound object from playing…_mySound.close();

Creating a play list• An array can be used to hold a list of mp3 file names.• A dynamic text field can be used to display a list of

the track names. • An input text field can be used to allow the user to

enter the number of the required track.• The play button event handler can be written to read

the number entered into the input text field by the user and to use it as the array index number to find the filename of the required track.

Suggested private variables

private var mySound:Sound;

private var track:Array = [“tracknameA.mp3", “tracknameB.mp3", “tracknameC.mp3” ];

private var userInputTxt:TextField;

The array of track file names// An array named track is defined by…private var track:Array = [“tracknameA.mp3",

“tracknameB.mp3", “tracknameC.mp3” ];

track[0] tracknameA.mp3

tracknameB.mp3

tracknameC.mp3

track[1]

track[2]

Array named track

Array index numbers, notice how the index number of the first element is 0 not 1.

Selecting a trackprivate function onMouseClick( event:MouseEvent ):void{

// Get the track number character entered by the user in the user input text field and convert to a number so it can be used as the array index

var trackNumber:uint = parseInt(userInputTxt.text);

// Access the array to get the required mp3 file name

var soundFileName:String = track[trackNumber];

// Access the external sound file in the usual way

var request:URLRequest = new URLRequest(soundFileName);_mySound = new Sound();_mySound.load(request);_mySound.play();

}