• home
  • forum
  • my
  • kt
  • download
  • AS 3.0 XML MP3 Player

    Author: 2009-04-27 11:19:01 From:

    Hey, I came back with something new and interesting, I make a Mp3 Player. Is not a very advenced but is not so easy to understud if you are new in this. Here is the code, and if you whant to ask me something leave me a comment.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    
    /**
    	 * Author: Horatiu Condrea ( http://www.flashdesign-store.com )
    	 * Description: AS 3.0 MP3 Player
    **/
     
        // -------------------------------------
        // Imports
        // -------------------------------------
     
    //I import the TweenMax class because, she helps me to make same tween in the stage
    import gs.TweenMax;
    import gs.easing.*;
     
    stage.scaleMode = StageScaleMode.NO_SCALE;
     
        // -------------------------------------
        // Variables
        // -------------------------------------
     
    var sound:Sound = new Sound();
    var musicReq:URLRequest;
     
    //The BytesArray helps me to "see" the sound bytes and the transform to a spectrum
    var ba:ByteArray = new ByteArray();
    var buffer:SoundLoaderContext;
    var sc:SoundChannel;
    var st:SoundTransform;
    var isPlaying:Boolean = true;
    var pos:Number;
    var currentSound:Sound = sound;
     
    var xml:XML;
    var songlist:XMLList;
    var currentIndex:Number = 0;
     
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, whenLoaded);
     
    //Here we load in our XML and create a function that will run when our loader has completed loading
     
    function whenLoaded(e:Event):void
    {
    	xml = new XML(e.target.data);
    	songlist = xml.song;
    	musicReq = new URLRequest(songlist[0].url);
    	sound.load(musicReq);
    	sc = sound.play();
    	title_txt.text = songlist[0].title; //I load the <title> of the XML file
    	nr_txt.text = songlist[0].number;   //I load the <number> of the XML file
    	gen_txt.text = songlist[0].gen;     //I load the <gen> of the XML file
    }
    //My XML file name is "main.xml" and in this part I load the XML
    loader.load(new URLRequest("main.xml"));
     
    // I created these 2 variables(bmd and bm)
    // Because the filter must know where to act
    var bmd:BitmapData = new BitmapData(512, 300, true, 0x000000);
    var bm:Bitmap = new Bitmap(bmd);
    addChild(bm);
     
    //I create a new Sprite for the spectrum line
    var sp:Sprite = new Sprite();
    addChild(sp);
     
    var blur:BlurFilter = new BlurFilter(8,8,2); // Set the BlurFilter Effect
     
    var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter
    ([1 ,0, 0, 0, 0,
      0, 1, 0, 0, 0,           // Set the ColorMatrixFilter Effect
      0, 0, 1, 0, 0,
      0, 0, 0, 0.96, 0]);
     
        // -------------------------------------
        // Stage Objects
        // -------------------------------------
     
    //Well in this part I put the objects on the stage,my object are Exporting to ActionScript, and the objects are in my library.
    //Some mathematical stufe
    var bar_a = new Bar_a();
    addChild(bar_a);
    bar_a.y = stage.stageHeight * 0.5 - 25;
    bar_a.x = stage.stageWidth * 0.5;
     
    var bar_b = new Bar_b();
    addChild(bar_b);
    bar_b.y = stage.stageHeight * 0.5 + 25;
    bar_b.x = stage.stageWidth * 0.5;
     
    var play_btn = new Play();
    addChild(play_btn);
    play_btn.y = stage.stageHeight * 0.5 - 15;
    play_btn.x = 80;
    play_btn.scaleX = 1.2;
    play_btn.scaleY = 1.2;
     
    var stop_btn = new Stop();
    addChild(stop_btn);
    stop_btn.y = stage.stageHeight * 0.5 - 15;
    stop_btn.x = 40;
    stop_btn.scaleX = 1.2;
    stop_btn.scaleY = 1.2;
     
    var pause_btn = new Pause();
    addChild(pause_btn);
    pause_btn.y = stage.stageHeight * 0.5 - 15;
    pause_btn.x = 60;
    pause_btn.scaleX = 1.2;
    pause_btn.scaleY = 1.2;
     
    var next_btn = new Next();
    addChild(next_btn);
    next_btn.y = stage.stageHeight * 0.5 - 15;
    next_btn.x = 100;
    next_btn.scaleX = 1.2;
    next_btn.scaleY = 1.2;
     
    var prev_btn = new Prev();
    addChild(prev_btn);
    prev_btn.y = stage.stageHeight * 0.5 - 11;
    prev_btn.x = 20;
    prev_btn.scaleX = 1.2;
    prev_btn.scaleY = 1.2;
     
    var volume_mc = new Volume();
    addChild(volume_mc);
    volume_mc.y = stage.stageHeight * 0.5 + 10;
    volume_mc.x = 325;
     
    buffer = new SoundLoaderContext(5000)
     
        // -------------------------------------
        // Buttons &#038; Function
        // -------------------------------------
     
    //Now we'll add some Mouse Events for our buttons on the MP3 Player and give them some functions
     
    stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
    function stopSound(e:MouseEvent):void
    {
    	SoundMixer.stopAll(); //With this function I stop all the music
    	isPlaying = false;
    }
     
    pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
    function pauseSound(e:MouseEvent):void
    {
    	//Well, if you look over the variables, I will find the "pos:number" variable, this will take the muisc position and then I stope the music because is a pause_btn
    	pos = sc.position;
    	sc.stop();
    	isPlaying = false;
    }
     
    play_btn.addEventListener(MouseEvent.CLICK, playSound);
    function playSound(e:MouseEvent):void
    {
    	//Here I start the muzic, but I start the muisc in the "pos" position
    	if(isPlaying == false)
    	{
    		sc = currentSound.play(pos);
     
    		isPlaying = true;
    	}
     
    }
     
    next_btn.addEventListener(MouseEvent.CLICK, nextSong);
    function nextSong(e:Event):void
    //Well in this function I start the next song, if you are clicking on the next_btn
    {
    	if (currentIndex < (songlist.length() + 1))
    	{
    		currentIndex++;
    	}
    	else
    	{
    		currentIndex = 0;
    	}
     
    	var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url);
    	var nextTitle:Sound = new Sound(nextReq);
    	sc.stop();
    	title_txt.text = songlist[currentIndex].title;//I load the<title>of the XML file
    	nr_txt.text = songlist[currentIndex].number;//I load the<number>of the XML file
    	gen_txt.text = songlist[currentIndex].gen;//I load the <gen> of the XML file
    	sc = nextTitle.play();
    	isPlaying = true;
    	currentSound = nextTitle;
    }
     
    prev_btn.addEventListener(MouseEvent.CLICK, prevSong);
    function prevSong(e:Event):void
    //Here I start the previos song
    {
    	if (currentIndex < (songlist.length() - 1))
    	{
    		currentIndex++;
    	}
    	else
    	{
    		currentIndex = 0;
    	}
     
    	var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url);
    	var nextTitle:Sound = new Sound(nextReq);
    	sc.stop();
    	title_txt.text = songlist[currentIndex].title;
    	nr_txt.text = songlist[currentIndex].number;//Again load the number, gen and title of the XML file
    	gen_txt.text = songlist[currentIndex].gen;
    	sc = nextTitle.play();
    	isPlaying = true;
    	currentSound = nextTitle;
    }
     
        // -------------------------------------
        // Spectrums
        // -------------------------------------
     
    addEventListener(Event.ENTER_FRAME, loop_a);
    addEventListener(Event.ENTER_FRAME, loop_b);
     
    function loop_a(e:Event):void
    //Well here is the first spectrum
    {
    	sp.graphics.clear();
    	sp.graphics.lineStyle(2, 0xfffff0);//Select a color and a thickness for the line spectrum
    	sp.graphics.moveTo(-5, 50); //I put my target for line
    	SoundMixer.computeSpectrum(ba);
    	for(var i:uint=0; i<256; i++)
    	{
    		var num:Number = -ba.readFloat()*50 + 50;
    		sp.graphics.lineTo(i*2, num);
    	}
    	bmd.draw(sp);
    	// Apply the filter effects to line
    	bmd.applyFilter(bmd,bmd.rect,new Point(),blur);
    	bmd.applyFilter(bmd,bmd.rect,new Point(),colorMatrix);
    	bmd.scroll(3,0);
    }
     
    function loop_b(e:Event):void
    //Here is the second spectrum, and it is make with the same way
    {
    	sp.graphics.clear();
    	sp.graphics.lineStyle(2, 0xfffff0);
    	sp.graphics.moveTo(-1, 250);
    	SoundMixer.computeSpectrum(ba);
    	for(var i:uint=0; i<256; i++)
    	{
    		var num:Number = -ba.readFloat()*50 + 250;
    		sp.graphics.lineTo(i*2, num);
    	}
    	bmd.draw(sp);
    	bmd.applyFilter(bmd,bmd.rect,new Point(),blur);
    	bmd.applyFilter(bmd,bmd.rect,new Point(),colorMatrix);
    	bmd.scroll(3,0);
    }
     
        // -------------------------------------
        // Progrss Bar
        // -------------------------------------
     
    progress_mc.addEventListener(MouseEvent.CLICK, setNewProgress);
    addEventListener(Event.ENTER_FRAME, calcProgress);
     
    function calcProgress(e:Event):void
    //In this function I caluclate the music progress for the Progrss Bar
    {
    	var p:MovieClip = this.progress_mc.mcProgressFill;
    	var w:uint		= Math.round( 175 * sc.position / currentSound.length);
    	p.width 		= w;
    }
     
    function setNewProgress(e:MouseEvent):void
    {
    	var t:uint = currentSound.length * e.currentTarget.mouseX / 175;
    	sc.stop();
    	sc = currentSound.play(t);
    }
     
        // -------------------------------------
        // Volume
        // -------------------------------------
     
    var drag:Boolean = false;
    var rectangle:Rectangle = new Rectangle(0,0,150,0);//I create a new rectangle
    volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
    stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
     
    volume_mc.slider_mc.buttonMode = true;
     
    function dragIt(e:Event):void
    {
    	volume_mc.slider_mc.startDrag(false,rectangle);
    	drag = true;
    	volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);
    }
     
    function adjustVolume(e:Event):void
    {
    	var vol:Number = volume_mc.slider_mc.x / 150;
    	var st:SoundTransform = new SoundTransform(vol);
    	if (sc != null)
    	{
    		sc.soundTransform = st;
    	}
    }
     
    function dropIt(e:Event):void
    {
    	if (drag)
    	{
    		volume_mc.slider_mc.stopDrag();
    		drag = false;
    	}
    }
     
        // -------------------------------------
        // Buttons Effects
        // -------------------------------------
     
    //Well, here is going to helps me the TweenMax, Here is runing the same effect, but it is put in the all buttons
    play_btn.addEventListener(MouseEvent.ROLL_OVER, rollover)
    function rollover(e:MouseEvent):void
    {
    	TweenMax.to(play_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    play_btn.addEventListener(MouseEvent.ROLL_OUT, rollout)
    function rollout(e:MouseEvent):void
    {
    	TweenMax.to(play_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
     
    stop_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_b)
    function rollover_b(e:MouseEvent):void
    {
    	TweenMax.to(stop_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    stop_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_b)
    function rollout_b(e:MouseEvent):void
    {
    	TweenMax.to(stop_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
     
    pause_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_c)
    function rollover_c(e:MouseEvent):void
    {
    	TweenMax.to(pause_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    pause_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_c)
    function rollout_c(e:MouseEvent):void
    {
    	TweenMax.to(pause_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
     
    next_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_d)
    function rollover_d(e:MouseEvent):void
    {
    	TweenMax.to(next_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    next_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_d)
    function rollout_d(e:MouseEvent):void
    {
    	TweenMax.to(next_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
     
    prev_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_e)
    function rollover_e(e:MouseEvent):void
    {
    	TweenMax.to(prev_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    prev_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_e)
    function rollout_e(e:MouseEvent):void
    {
    	TweenMax.to(prev_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
     
    volume_mc.slider_mc.addEventListener(MouseEvent.ROLL_OVER, rollover_f)
    function rollover_f(e:MouseEvent):void
    {
    	TweenMax.to(volume_mc.slider_mc, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}});
    }
     
    volume_mc.slider_mc.addEventListener(MouseEvent.ROLL_OUT, rollout_f)
    function rollout_f(e:MouseEvent):void
    {
    	TweenMax.to(volume_mc.slider_mc, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}});
    }
    stop();

    discuss this topic to forum

    relation tutorial

    No information

    Category

      3D (33)
      Math Physics (18)
      3rd Party (10)
      Navigation (70)
      Actionscripting (201)
      Optimization (17)
      Animation (158)
      Projector (11)
      Audio (54)
      Special Effects (169)
      Backend (26)
      Text Effects (89)
      Drawing (33)
      Tips and Techniques (51)
      Dynamic Content (34)
      Tricks (8)
      Games (103)
      Utilities (23)
      Getting Started (95)
      Video (58)
      Interactivity (45)
      Web Design (34)

    New

    Hot