FACILITATING AUDIO AND VIDEO
471
HTTP Video Streaming
A new popular solution is HTTP video streaming, which is sometimes called PHP streaming since PHP is
mostly used for this solution. The idea is to overcome the limitations of progressive download and offer
streaming without the need to upload software to a server that’s usually expensive and complicated to
install. This is accomplished by using a small server-side script.Progressive download doesn’t allow
seeking to a location that hasn’t yet been downloaded; HTTP streaming handles seeking differently. When
a seek operation is performed, the application makes a request to the server-side script to play the file
starting from a certain position. The script then starts the video from the offset given.
Selecting the Right Type of Server
To know whether you need a web server or a media server, you need to understand what each offers.
Table 14-1 shows the relative advantages of each, so you can select the most appropriate type of server
for a given purpose. Keep in mind that media servers offer HTTP connections. It is also important to know
that the cost for a media smedia server is high.
Table 14-1. The Relative Advantages of Media and Web Servers
Parameter Media Server Web Server
* Short video - +
Allow user to copy video - +
Ensure data integrity - +
Long video + -
Interactivity + -
Social features + -
Live WebCam + -
Advanced user’s features + -
Cost - +
* Note that mobile devices may lose network connections and packets of data often, so while it’s generally
preferable to deploy short videos on a web server, at times it may be better to stream short videos.
Creating a Music Player
Now let’s create a custom music player generic enough to be used with any application, and then use it to
play music files.
CHAPTER 14
472
You can download the complete code for the Music Player API from http://github.com/EladElrom/
eladlib/blob/master/EladLibFlex/src/PlayerExample.mxml which includes the code and an example of a
working music player.
IPlayer
A good place to start is the interface. The music player interface, IPlayer, is the contract the music player
will obey. It includes methods the music player needs, such as pausing the music player or playing a track.
package com.elad.framework.musicplayer
{
public interface IPlayer
{
function playTrack(songUrl:String, songLenght:Number=0):void
function pauseTrack():void
function stopTrack():void
function fastforward(timeInSeconds:Number=2):void
function rewind(timeInSeconds:Number=2):void
function setVolume(vol:Number):void
}
}
AbstractPlayer
The music player’s abstract class AbstractPlayer, just like any other abstract class, should not be
instantiated. The abstract class is the parent class from which the music player is derived. The class
includes methods that any subclass will need; as well as incomplete methods, which will be left
unimplemented.
Although AS3 doesn’t really support “real” Abstract, developers should resist instantiating Abstract classes
or use one of the available hacks (such as creating a singleton) to ensure the abstract class doesn’t get
instantiated. These solutions are hack-ish, but they get the job done
package com.elad.framework.musicplayer
{
import flash.events.EventDispatcher;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.utils.Timer;
public class AbstractPlayer extends EventDispatcher
{
The Sound class lets us load and play an external file, while SoundChannel allows us to assign a song to
a sound channel.
protected var sound:Sound;
protected var channel:SoundChannel;
FACILITATING AUDIO AND VIDEO
473
We’ll use a timer to keep track of the sound position and fileBytesTotal to keep track of the bytes
loaded.
protected var soundPosition:Timer = null;
protected var fileBytesTotal:Number;
isDownloadCompleted is a flag indicates whether the download of the file has completed.
protected var isDownloadCompleted:Boolean = false;
Once the file is downloaded, the variable pausePosition will hold the pause position
protected var pausePosition:Number;
The isPause flag indicates whether the track is in pause mode.
protected var isPause:Boolean = false;
The isPlaying flag indicates whether the track is currently playing.
protected var isPlaying:Boolean = false;
The _songPosition variable holds the play position in seconds. Note that we will be using the underscore
to indicate that the variable is private and has a setter and getter.
private var _songPosition:Number;
The variable _ songLength holds the total song length in seconds.
private var _songLength:Number;
The variable _songURL holds the song we will be streaming.
private var _songURL:String;
Setters and getters are defined below:
public function get songPosition():Number
{
return _songPosition;
}
public function set songPosition(val:Number):void
{
_songPosition = val;
}
public function get songLength():Number
{
return _songLength;
}
public function set songLength(val:Number):void
{
_songLength = val;
}
public function get songURL():String
{
Get AdvancED Flex 4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.