{lang: 'de'}
Flashhilfe.de - Flash Community

Xml Playlist in AS integrieren [Flash 10]

 


AntwortenRegistrieren Seite1  

daniels0608#1
Benutzerbild von daniels0608
Beiträge: 4
Registriert: Dec 2010

29.03.2012, 21:39

Hallo zusammen,

ich habe einen Videoplayer der mehrere Videos hintereinander abspielen kann, diese Videos werden über eine Xml Datei geladen. Jetzt muss ich den Videopalyer aber so anpassen das er ohne xml auskommt, also die Videos im AS selber geladen werden. Ich bin jetzt nicht ganz so fit in AS3, deshalb wollte ich hier mal Fragen ob mir da jemand einen Tip geben kann wie ich das umsetzen kann.

Ich habe auch die Datei angehängt

ActionScript:
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// ###############################
// ############# CONSTANTS
// ###############################

// time to buffer for the video in sec.
const BUFFER_TIME:Number            = 8;
// start volume when initializing player
const DEFAULT_VOLUME:Number            = 0.6;
// update delay in milliseconds.
const DISPLAY_TIMER_UPDATE_DELAY:int   = 10;
// smoothing for video. may slow down old computers
const SMOOTHING:Boolean               = true;


// ###############################
// ############# VARIABLES
// ###############################

// flag for knowing if user hovers over description label
var bolDescriptionHover:Boolean = false;
// flag for knowing in which direction the description label is currently moving
var bolDescriptionHoverForward:Boolean = true;
// flag for knowing if flv has been loaded
var bolLoaded:Boolean               = false;
// flag for volume scrubbing
var bolVolumeScrub:Boolean            = false;
// flag for progress scrubbing
var bolProgressScrub:Boolean         = false;
// holds the number of the active video
var intActiveVid:int;
// holds the last used volume, but never 0
var intLastVolume:Number            = DEFAULT_VOLUME;
// net connection object for net stream
var ncConnection:NetConnection;
// net stream object
var nsStream:NetStream;
// object holds all meta data
var objInfo:Object;
// shared object holding the player settings (currently only the volume)
var shoVideoPlayerSettings:SharedObject = SharedObject.getLocal("playerSettings");
// url to flv file
var strSource:String               = root.loaderInfo.parameters.playlist == null ? "playlist.xml" : root.loaderInfo.parameters.playlist;
// timer for updating player (progress, volume...)
var tmrDisplay:Timer;
// loads the xml file
var urlLoader:URLLoader;
// holds the request for the loader
var urlRequest:URLRequest;
// playlist xml
var xmlPlaylist:XML;

// ###############################
// ############# STAGE SETTINGS
// ###############################
stage.scaleMode   = StageScaleMode.NO_SCALE;
stage.align      = StageAlign.TOP_LEFT;

// ###############################
// ############# FUNCTIONS
// ###############################

// sets up the player
function initVideoPlayer():void {
     // hide video controls on initialisation
   mcVideoControls.visible = false;
     
     // hide buttons
   mcVideoControls.btnUnmute.visible         = false;
     mcVideoControls.btnPause.visible         = false;
     mcVideoControls.btnFullscreenOff.visible   = false;
     
     // set the progress/preload fill width to 1
   mcVideoControls.mcProgressFill.mcFillRed.width   = 1;
     mcVideoControls.mcProgressFill.mcFillGrey.width   = 1;
     
     // set time and duration label
   mcVideoControls.lblTimeDuration.htmlText      = "<font color='#ffffff'>00:00</font> / 00:00";
     
     // add global event listener when mouse is released
   stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
     
     // add fullscreen listener
   stage.addEventListener(FullScreenEvent.FULL_SCREEN, onFullscreen);
        
     // add event listeners to all buttons
   mcVideoControls.btnPause.addEventListener(MouseEvent.CLICK, pauseClicked);
     mcVideoControls.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);
     mcVideoControls.btnStop.addEventListener(MouseEvent.CLICK, stopClicked);
     mcVideoControls.btnNext.addEventListener(MouseEvent.CLICK, playNext);
     mcVideoControls.btnPrevious.addEventListener(MouseEvent.CLICK, playPrevious);
     mcVideoControls.btnMute.addEventListener(MouseEvent.CLICK, muteClicked);
     mcVideoControls.btnUnmute.addEventListener(MouseEvent.CLICK, unmuteClicked);
     mcVideoControls.btnFullscreenOn.addEventListener(MouseEvent.CLICK, fullscreenOnClicked);
     mcVideoControls.btnFullscreenOff.addEventListener(MouseEvent.CLICK, fullscreenOffClicked);
           
     mcVideoControls.btnVolumeBar.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);
     mcVideoControls.mcVolumeScrubber.btnVolumeScrubber.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);
     mcVideoControls.btnProgressBar.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);
     mcVideoControls.mcProgressScrubber.btnProgressScrubber.addEventListener(MouseEvent.MOUSE_DOWN, progressScrubberClicked);
 
     mcVideoControls.mcVideoDescription.btnDescription.addEventListener(MouseEvent.MOUSE_OVER, startDescriptionScroll);
     mcVideoControls.mcVideoDescription.btnDescription.addEventListener(MouseEvent.MOUSE_OUT, stopDescriptionScroll);
     
     // create timer for updating all visual parts of player and add
   // event listener
   tmrDisplay = new Timer(DISPLAY_TIMER_UPDATE_DELAY);
     tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);
     
     // create a new net connection, add event listener and connect
   // to null because we don't have a media server
   ncConnection = new NetConnection();
     ncConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
     ncConnection.connect(null);
     
     // create a new netstream with the net connection, add event
   // listener, set client to this for handling meta data and
   // set the buffer time to the value from the constant
   nsStream = new NetStream(ncConnection);
     nsStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
     nsStream.client = this;
     nsStream.bufferTime = BUFFER_TIME;
     
     // attach net stream to video object on the stage
   vidDisplay.attachNetStream(nsStream);
     // set the smoothing value from the constant
   vidDisplay.smoothing = SMOOTHING;
     
     // set default volume and get volume from shared object if available
   var tmpVolume:Number = DEFAULT_VOLUME;
     if(shoVideoPlayerSettings.data.playerVolume != undefined) {
        tmpVolume = shoVideoPlayerSettings.data.playerVolume;
        intLastVolume = tmpVolume;
     }
     // update volume bar and set volume
   mcVideoControls.mcVolumeScrubber.x = (53 * tmpVolume) + 318;
     mcVideoControls.mcVolumeFill.mcFillRed.width = mcVideoControls.mcVolumeScrubber.x - 371 + 53;
     setVolume(tmpVolume);
     
     // create new request for loading the playlist xml, add an event listener
   // and load it
   urlRequest = new URLRequest(strSource);
     urlLoader = new URLLoader();
     urlLoader.addEventListener(Event.COMPLETE, playlistLoaded);
     urlLoader.load(urlRequest);
}
function playClicked(e:MouseEvent):void {
     // check's, if the flv has already begun
   // to download. if so, resume playback, else
   // load the file
   if(!bolLoaded) {
        nsStream.play(strSource);
        bolLoaded = true;
     }
     else{
        nsStream.resume();
     }
     
     vidDisplay.visible = true;
     
     // switch play/pause visibility
   mcVideoControls.btnPause.visible   = true;
     mcVideoControls.btnPlay.visible      = false;
}

function pauseClicked(e:MouseEvent):void {
     // pause video
   nsStream.pause();
     
     // switch play/pause visibility
   mcVideoControls.btnPause.visible   = false;
     mcVideoControls.btnPlay.visible      = true;
}

function stopClicked(e:MouseEvent):void {
     // calls stop function
   stopVideoPlayer();
}

function muteClicked(e:MouseEvent):void {
     // set volume to 0
   setVolume(0);
     
     // update scrubber and fill position/width
   mcVideoControls.mcVolumeScrubber.x            = 318;
     mcVideoControls.mcVolumeFill.mcFillRed.width   = 1;
}

function unmuteClicked(e:MouseEvent):void {
     // set volume to last used value or DEFAULT_VOLUME if last volume is zero
   var tmpVolume:Number = intLastVolume == 0 ? DEFAULT_VOLUME : intLastVolume
     setVolume(tmpVolume);
     
     // update scrubber and fill position/width
   mcVideoControls.mcVolumeScrubber.x = (53 * tmpVolume) + 318;
     mcVideoControls.mcVolumeFill.mcFillRed.width = mcVideoControls.mcVolumeScrubber.x - 371 + 53;
}

function volumeScrubberClicked(e:MouseEvent):void {
     // set volume scrub flag to true
   bolVolumeScrub = true;
     
     // start drag
   mcVideoControls.mcVolumeScrubber.startDrag(true, new Rectangle(318, 19, 53, 0)); // NOW TRUE
}

function progressScrubberClicked(e:MouseEvent):void {
     // set progress scrub flag to true
   bolProgressScrub = true;
     
     // start drag
   mcVideoControls.mcProgressScrubber.startDrag(true, new Rectangle(0, 2, 432, 0)); // NOW TRUE
}

function mouseReleased(e:MouseEvent):void {
     // set progress/volume scrub to false
   bolVolumeScrub      = false;
     bolProgressScrub   = false;
     
     // stop all dragging actions
   mcVideoControls.mcProgressScrubber.stopDrag();
     mcVideoControls.mcVolumeScrubber.stopDrag();
     
     // update progress/volume fill
   mcVideoControls.mcProgressFill.mcFillRed.width   = mcVideoControls.mcProgressScrubber.x + 5;
     mcVideoControls.mcVolumeFill.mcFillRed.width   = mcVideoControls.mcVolumeScrubber.x - 371 + 53;
     
     // save the volume if it's greater than zero
   if((mcVideoControls.mcVolumeScrubber.x - 318) / 53 > 0)
        intLastVolume = (mcVideoControls.mcVolumeScrubber.x - 318) / 53;
}

function updateDisplay(e:TimerEvent):void {
     // checks, if user is scrubbing. if so, seek in the video
   // if not, just update the position of the scrubber according
   // to the current time
   if(bolProgressScrub)
        nsStream.seek(Math.round(mcVideoControls.mcProgressScrubber.x * objInfo.duration / 432))
     else
        mcVideoControls.mcProgressScrubber.x = nsStream.time * 432 / objInfo.duration;
     
     // set time and duration label
   mcVideoControls.lblTimeDuration.htmlText      = "<font color='#ffffff'>" + formatTime(nsStream.time) + "</font> / " + formatTime(objInfo.duration);
     
     // update the width from the progress bar. the grey one displays
   // the loading progress
   mcVideoControls.mcProgressFill.mcFillRed.width   = mcVideoControls.mcProgressScrubber.x + 5;
     mcVideoControls.mcProgressFill.mcFillGrey.width   = nsStream.bytesLoaded * 438 / nsStream.bytesTotal;
     
     // update volume and the red fill width when user is scrubbing
   if(bolVolumeScrub) {
        setVolume((mcVideoControls.mcVolumeScrubber.x - 318) / 53);
        mcVideoControls.mcVolumeFill.mcFillRed.width = mcVideoControls.mcVolumeScrubber.x - 371 + 53;
     }
     
     // chech if user is currently hovering over description label
   if(bolDescriptionHover) {
        // check in which direction we're currently moving
      if(bolDescriptionHoverForward) {
           // move to the left and check if we've shown everthing
         mcVideoControls.mcVideoDescription.lblDescription.x -= 0.1;
           if(mcVideoControls.mcVideoDescription.lblDescription.textWidth - 133 <= Math.abs(mcVideoControls.mcVideoDescription.lblDescription.x))
              bolDescriptionHoverForward = false;
        } else {
           // move to the right and check if we're back to normal
         mcVideoControls.mcVideoDescription.lblDescription.x += 0.1;
           if(mcVideoControls.mcVideoDescription.lblDescription.x >= 0)
              bolDescriptionHoverForward = true;
        }
     } else {
        // reset label position and direction variable
      mcVideoControls.mcVideoDescription.lblDescription.x = 0;
        bolDescriptionHoverForward = true;
     }
}

function onMetaData(info:Object):void {
     // stores meta data in a object
   objInfo = info;
     
     // now we can start the timer because
   // we have all the neccesary data
   if(!tmrDisplay.running)
        tmrDisplay.start();
}

function netStatusHandler(event:NetStatusEvent):void {
     // handles net status events
   switch (event.info.code) {
        // trace a messeage when the stream is not found
      case "NetStream.Play.StreamNotFound":
           trace("Stream not found: " + strSource);
        break;
        // when the video reaches its end, we check if there are
      // more video left or stop the player
      case "NetStream.Play.Stop":
           if(intActiveVid + 1 < xmlPlaylist..vid.length())
              playNext();
           else
              stopVideoPlayer();
        break;
     }
}

function stopVideoPlayer():void {
     // pause netstream, set time position to zero
   nsStream.pause();
     nsStream.seek(0);
     
     // in order to clear the display, we need to
   // set the visibility to false since the clear
   // function has a bug
   vidDisplay.visible               = false;
     
     // switch play/pause button visibility
   mcVideoControls.btnPause.visible   = false;
     mcVideoControls.btnPlay.visible      = true;
}

function setVolume(intVolume:Number = 0):void {
     // create soundtransform object with the volume from
   // the parameter
   var sndTransform      = new SoundTransform(intVolume);
     // assign object to netstream sound transform object
   nsStream.soundTransform   = sndTransform;
     
     // hides/shows mute and unmute button according to the
   // volume
   if(intVolume > 0) {
        mcVideoControls.btnMute.visible      = true;
        mcVideoControls.btnUnmute.visible   = false;
     } else {
        mcVideoControls.btnMute.visible      = false;
        mcVideoControls.btnUnmute.visible   = true;
     }
     
     // store the volume in the flash cookie
   shoVideoPlayerSettings.data.playerVolume = intVolume;
     shoVideoPlayerSettings.flush();
}

function formatTime(t:int):String {
     // returns the minutes and seconds with leading zeros
   // for example: 70 returns 01:10
   var s:int = Math.round(t);
     var m:int = 0;
     if (s > 0) {
        while (s > 59) {
           m++; s -= 60;
        }
        return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);
     } else {
        return "00:00";
     }
}

function fullscreenOnClicked(e:MouseEvent):void {
     // go to fullscreen mode
   stage.displayState = StageDisplayState.FULL_SCREEN;
}

function fullscreenOffClicked(e:MouseEvent):void {
     // go to back to normal mode
   stage.displayState = StageDisplayState.NORMAL;
}

function onFullscreen(e:FullScreenEvent):void {
     // check if we're entering or leaving fullscreen mode
    if (e.fullScreen) {
        // switch fullscreen buttons
        mcVideoControls.btnFullscreenOn.visible = false;
        mcVideoControls.btnFullscreenOff.visible = true;
        
        // bottom center align controls
      mcVideoControls.x = (Capabilities.screenResolutionX - 440) / 2;
        mcVideoControls.y = (Capabilities.screenResolutionY - 33);
        
        // size up video display
      vidDisplay.height    = (Capabilities.screenResolutionY - 33);
        vidDisplay.width    = vidDisplay.height * 4 / 3;
        vidDisplay.x      = (Capabilities.screenResolutionX - vidDisplay.width) / 2;
      } else {
        // switch fullscreen buttons
        mcVideoControls.btnFullscreenOn.visible = true;
        mcVideoControls.btnFullscreenOff.visible = false;
        
        // reset controls position
      mcVideoControls.x = 0;
        mcVideoControls.y = 330;
        
        // reset video display
      vidDisplay.y = 0;
        vidDisplay.x = 0;
        vidDisplay.width = 440;
        vidDisplay.height = 330;
      }
}

function playlistLoaded(e:Event):void {
     // create new xml with loaded data from loader
   xmlPlaylist = new XML(urlLoader.data);
     
     // set source of the first video but don't play it
   playVid(0, false)
     
     // show controls
   mcVideoControls.visible = true;
}

function playVid(intVid:int = 0, bolPlay = true):void {
     if(bolPlay) {
        // stop timer
      tmrDisplay.stop();
        
        // play requested video
      nsStream.play(String(xmlPlaylist..vid[intVid].@src));
        
        // switch button visibility
      mcVideoControls.btnPause.visible   = true;
        mcVideoControls.btnPlay.visible      = false;
     } else {
        strSource = xmlPlaylist..vid[intVid].@src;
     }
     
     // show video display
   vidDisplay.visible               = true;
     
     // reset description label position and assign new description
   mcVideoControls.mcVideoDescription.lblDescription.x = 0;
     mcVideoControls.mcVideoDescription.lblDescription.htmlText = (intVid + 1) + ". <font color='#ffffff'>" + String(xmlPlaylist..vid[intVid].@desc) + "</font>";
     
     // update active video number
   intActiveVid = intVid;
}

function playNext(e:MouseEvent = null):void {
     // check if there are video left to play and play them
   if(intActiveVid + 1 < xmlPlaylist..vid.length())
        playVid(intActiveVid + 1);
        
}

function playPrevious(e:MouseEvent = null):void {
     // check if we're not and the beginning of the playlist and go back
   if(intActiveVid - 1 >= 0)
        playVid(intActiveVid - 1);
}

function startDescriptionScroll(e:MouseEvent):void {
     // check if description label is too long and we need to enable scrolling
   if(mcVideoControls.mcVideoDescription.lblDescription.textWidth > 138)
        bolDescriptionHover = true;
}

function stopDescriptionScroll(e:MouseEvent):void {
     // disable scrolling
   bolDescriptionHover = false;
}

// ###############################
// ############# INIT PLAYER
// ###############################
initVideoPlayer();


Angehängte Dateien:
Komprimierte Datei videoplayer-xml.rar74.69 KB
Schlagwörter: Videoplayer, xml playliste
cedddy#2
Benutzerbild von cedddy
Beiträge: 572
Registriert: May 2007

02.05.2012, 21:57