Hi guys,
I’m working on a MusicPlayer class. It’s my first little project with a design pattren.
Most of my knolige comes from the essential actionscript 2.0 book.
I want to add a posibility, that makes it posible to add a playlist later, without using a view.
But the methods to add a playlist (addPlaylist) is positioned in the model (MusicPlayerModel).
This is how you create the class at the moment:
var playList:Array = [{title:"treeDance",url:"sounds/treeDance.mp3"},{title:"The Voyage",url:"sounds/The Voyage.mp3"},{title:"SmoothCore",url:"sounds/SmoothCore.mp3"}]
import nl.theFlashWizard.musicPlayer.MusicPlayer;
MusicPlayer.main(musicPlayer_mc,playList);
But as you can see, this won’t work when you get the playlist from xml.
How would you guys fix this problem?
Add an method to the MusicPlayer class that simply starts the addPlayList in the model? I was hoping that there’s a better way.
The MusicPlayer class:
/**
* The class that's initiates the music player.
* It creates the model and views and it adds the view as an observer to the model.
*/
import nl.theFlashWizard.musicPlayer.*;
import util.Observable;
class nl.theFlashWizard.musicPlayer.MusicPlayer extends Observable{
private var musicPlayerModel:MusicPlayerModel;
private var musicPlayerView:MusicPlayerView;
/**
* Constructor
* @param target The clip in witch to create the view assets.
* @param playList The playlist, an array with songs. Every array item is an object with a title and url variable.
*/
public function MusicPlayer(target:MovieClip,playList:Array){
musicPlayerModel = new MusicPlayerModel(playList);
musicPlayerView = new MusicPlayerView(musicPlayerModel,undefined,target);
musicPlayerModel.addObserver(musicPlayerView);
}
/**
* Create the music player.
* @param target The clip in witch to create the view assets.
* @param playList The playlist, an array with songs. Every array item is an object with a title and url variable.
*/
public static function main(target:MovieClip,playList:Array):Void{
var musicPlayer:MusicPlayer = new MusicPlayer(target,playList);
}
}
The MusicPlayerModel:
/**
* The Model
*/
import nl.theFlashWizard.util.Tools;
import nl.theFlashWizard.musicPlayer.*;
import util.Observable;
import ascb.util.Proxy;
import util.Observer;
class nl.theFlashWizard.musicPlayer.MusicPlayerModel extends Observable{
private var _music:Sound;
private var _playList:Array;
private var _playListCopy:Array;
private var _song:Number;
private var _repeat:Boolean;
private var _repeatAll:Boolean;
private var _shuffle:Boolean;
private var _state:String;
private var _volume:Number;
private var _isMute:Boolean;
private var _prevPos:Number;
private var _prevUrl:String;
/**
* Constructor
* @param playList The Playlist. An array with songs. Every array item is an object with a title and url variable.
*/
public function MusicPlayerModel(newPlayList:Array){
_music = new Sound();
playList = newPlayList
song = 1;
repeat = false;
repeatAll = true
shuffle = false
_state = "stopped"
isMute = false
volume = 50;
_music.onSoundComplete = Proxy.create(this, onSoundComplete);
}
/**
* The number of the current song.
*/
public function get song():Number{
return _song;
}
public function set song(newSong:Number):Void{
if(newSong == undefined || newSong == null) return;
if(repeatAll){
if(newSong > playList.length) newSong = 1;
else if(newSong < 1) newSong = playList.length;
}else{
newSong = Tools.limitIt(newSong,1,playList.length);
}
_song = newSong;
makeNotify();
}
/**
* The title of the current song.
*/
public function get title():String{
return playList[song-1].title;
}
/**
* The url to the current song.
*/
public function get url():String{
return playList[song-1].url;
}
/**
* The state of the music player. Playing/paused/stopped.
*/
public function get state():String{
return _state;
}
/**
* The playlist.
*/
public function get playList():Array{
return _playList;
}
public function set playList(newPlayList):Void{
_playList = new Array();
if(newPlayList != undefined) _playList = newPlayList;
}
/**
* If the music is paused.
*/
public function get isPause():Boolean{
if(state == "paused") return true;
else return false;
}
public function set isPause(setPause:Boolean):Void{
if(setPause){
pause();
}else{
resume();
}
}
//--------------------Music Control--------------------;
/**
* Start playing the current song.
*/
public function start():Void{
//Check if playlist isn't empty:
if(playList.length == 0){
this.stop();
return;
}
//When there's a different url the song needs to be loaded else its just play's the song.
if(playList[song-1].url != _prevUrl){
_music.loadSound(playList[song-1].url, true);
_prevUrl = playList[song-1].url;
}else{
_music.start(_prevPos);
}
volume = volume;
_prevPos = 0
_state = "playing";
makeNotify();
}
/**
* Stop playing the current song.
*/
public function stop():Void{
_prevPos = 0;
_music.stop();
_state = "stopped";
makeNotify();
}
/**
* Pause the music.
*/
private function pause():Void{
_prevPos = _music.position/1000;
_music.stop();
_state = "paused";
makeNotify();
}
/**
* Resume the music.
*/
private function resume():Void{
start();
}
//--------------------Afspeelopties--------------------;
/**
* If the song is repeated.
*/
public function get repeat():Boolean{
return _repeat;
}
public function set repeat(newRepeat:Boolean):Void{
if(newRepeat != undefined || newRepeat != null || newRepeat != _repeat){
_repeat = newRepeat;
}
}
/**
* If the playlist is repeated.
*/
public function get repeatAll():Boolean{
return _repeatAll;
}
public function set repeatAll(newRepeatAll:Boolean):Void{
if(newRepeatAll != undefined || newRepeatAll != null || newRepeatAll != _repeatAll){
_repeatAll = newRepeatAll;
}
}
/**
* If the playlist is shuffled.
*/
public function get shuffle():Boolean{
return _shuffle;
}
public function set shuffle(newShuffle:Boolean):Void{
if(newShuffle != undefined || newShuffle != null || newShuffle != shuffle){
_shuffle = newShuffle;
if(_shuffle){
shufflePlayList();
}else{
RestorePlayList();
}
}
makeNotify();
}
/**
* Shuffle the playlist.
*/
private function shufflePlayList():Void{
_playListCopy = Tools.dupArray(playList);
playList = Tools.shuffleObj(playList);
}
/**
* Restore the playlist.
*/
private function RestorePlayList():Void{
if(_playListCopy != undefined){
playList = _playListCopy;
}
}
/**
* When the song is completely played..
*/
function onSoundComplete():Void{
if(repeat){
start();
}else{
next();
}
}
//--------------------volume--------------------;
/**
* Get the volume of the music.
*/
public function get volume():Number{
return _volume;
}
public function set volume(setVolume:Number):Void{
if(!isMute){
_volume = Tools.limitIt(setVolume,0,100);
_music.setVolume(_volume);
makeNotify();
}
}
//--------------------mute--------------------;
/**
* If the music is muted.
*/
public function get isMute():Boolean{
return _isMute;
}
public function set isMute(setMute:Boolean):Void{
_isMute = setMute;
if(_isMute){
_music.setVolume(0);
}else{
_music.setVolume(_volume);
}
makeNotify();
}
//--------------------next/prev--------------------;
/**
* Select the next song
*/
public function next():Void{
song = song+1;
if(state == "playing") start(song);
}
/**
* Select the previous song
*/
public function prev():Void{
song = song-1;
if(state == "playing") start(song);
}
//--------------------playlist--------------------;
/**
* Add a song to the playlist.
* @param title The title of the song.
* @param url The url to the song.
*/
public function addSong(title:String,url:String):Void{
var song:Object = new Object();
song.title = title;
song.url = url;
var prevURL:String = url;
playList.push(song);
updateCurrentSong(prevURL);
}
/**
* Delete a song from the playlist.
* @param song The number
*/
public function delSong(song:Number):Void{
var prevURL:String = url;
var delSong:Number = song;
playList.splice(delSong-1,1);
if(delSong == song && state == "playing") start();
else updateCurrentSong(prevURL);
}
/**
* Delete the whole playlist.
*/
public function delSongs():Void{
playList = new Array();
this.stop();
}
/**
* Update the current song.
*/
private function updateCurrentSong(currentUrl:String):Void{
for(var i=0;i<playList.length;i++){
if(currentUrl == playList*.url){
song = i+1;
}
}
makeNotify();
}
//--------------------MVC stuff--------------------;
/**
* Adds an observer to the list of observers.
* @param o The observer to be added.
*/
public function addObserver(o:Observer):Boolean {
if (o == null) {
return false;
}
for (var i:Number = 0; i < observers.length; i++) {
if (observers* == o) {
return false;
}
}
observers.push(o);
makeNotify();
return true;
}
/*
//Het volgende werkt ook al niet helaas:
public function addObserver(o:Observer):Boolean {
var supersReturn:Boolean = super(o);
makeNotify();
return supersReturn;
}
*/
/**
* Collect the information and notify the observers/views.
*/
private function makeNotify():Void{
setChanged();
var infoObj:MusicPlayerUpdate = new MusicPlayerUpdate(state,volume,isMute,song,title,playList,repeat,repeatAll,shuffle);
notifyObservers(infoObj);
}
/**
* Unregister all the observers/views.
*/
public function destroy():Void{
var observersSnapshop:Array = observers.concat();
for(var i:Number = observersSnapshop.length-1;i >= 0;i--){
observersSnapshop*.releaseSubject();
}
}
}
You can find all the files here:
http://members.lycos.nl/tut1/uploadbestanden/musicPlayer.zip
■■■■ you’re as tags are working anoing…