Player de Vídeo em AS2.0 - Primeira Parte

Salve pessoal !

Hoje eu vou dar inicio a uma série de artigos onde vou abordar a construção de um player de vídeo, inicialmente vamos fazer esse player em AS2.0.
Nesse primeiro exercício não vamos usar OOP ( nada de classe ), faremos isso depois de construir todo o player para que todos possam acompanhar como é o processo de desenvolvimento passando por todas as fases.

Características do player desenvolvido:
Toca vídeos no formato FLV.
Indica quando o vídeo está em buffer.
Mostra o tempo total e o tempo de execução do vídeo.
Botão Play e Pause.

Próximas características a serem adicionadas
Controle de som
Modo Fullscreen
Lista de vídeos
Implementação do player em OOP

 

Feito isso no frame denominado “as” cole o código abaixo.

Actionscript:
  1. //Importando Delegate
  2. import mx.utils.Delegate;
  3. //definindo variáveis
  4. var conexao:NetConnection;
  5. var stream:NetStream;
  6. var total:Number;
  7. var mcSeek:MovieClip;
  8. var tamanho:Number;
  9. var txtTempo:TextField;
  10. var mcLoad:MovieClip;
  11. var btPlay:MovieClip;
  12. var videoContainer:Video
  13.  
  14. //Configuração do Player
  15. function PlayVideo() {
  16.  
  17. this.total = 1;
  18. this.tamanho = this.mcSeek._width;
  19.  
  20. this.conexao = new NetConnection();
  21. this.conexao.connect(null);
  22. this.stream = new NetStream(this.conexao);
  23. this.videoContainer.attachVideo(this.stream);
  24. this.stream.play("video.flv");
  25.  
  26. this.stream.onMetaData = Delegate.create(this, this.onNsMetaData);
  27. this.stream.onStatus = Delegate.create(this, this.onStatusVideo);
  28.  
  29. this.stream.setBufferTime(2);
  30. this.bufferClip._visible =true
  31.  
  32. this.btPlay.gotoAndStop(2)
  33. }
  34. //Verifica status do Vídeo
  35. function onStatusVideo(info:Object){
  36. //trace(info.code);
  37. if (info.code == "NetStream.Buffer.Full") {
  38. bufferClip._visible =false
  39. }
  40. if (info.code == "NetStream.Buffer.Empty") {
  41. stream.seek(0);
  42. pauseSincVideo()
  43. btPlay.gotoAndStop(1)
  44. }
  45. if (info.code == "NetStream.Play.Stop") {
  46. bufferClip._visible =false
  47. }
  48. }
  49. // Verifica Progresso
  50. function onNsMetaData(info:Object) {
  51. this.total = info.duration;
  52. this.mcSeek.onEnterFrame = this.progressoVideo;
  53. }
  54.  
  55. //Configura Prograsso
  56. function progressoVideo() {
  57. txtTempo.text = tempoVideo(stream.time)+" / "+tempoVideo(total);
  58. mcSeek._width = tamanho*(stream.time/total);
  59. mcLoad._width = tamanho*(stream.bytesLoaded/stream.bytesTotal);
  60. }
  61.  
  62. //Retorna o tempo total e o tempo
  63. //em andamento
  64. function tempoVideo(num:Number) {
  65. var minimo:Number = Math.floor(num/60);
  66. var segundos = Math.floor(num%60);
  67. if (segundos<10) {
  68. segundos = "0"+segundos;
  69. }
  70. return minimo+":"+segundos;
  71. }
  72.  
  73. //Play Vídeo
  74. function playSincVideo() {
  75. stream.pause(false);
  76. btPlay.gotoAndStop(2)
  77. }
  78. //Pause Video
  79. function pauseSincVideo() {
  80. stream.pause(true);
  81. btPlay.gotoAndStop(1)
  82.  
  83. }
  84. //Ação do botão PlayPause
  85. btPlay.onRelease = function() {
  86. if(this._currentframe ==1){
  87. _root.playSincVideo();
  88. }
  89. else{
  90. _root.pauseSincVideo();
  91. }
  92.  
  93. };
  94. //Inicia Processo
  95. PlayVideo();

Veja um exemplo do Player

Espero que vocês gostem, semana que vem tem a segunda parte do tutorial abraços !

2 Responses

  1. daniel romualdo:

    Gostaria de saber para que serve a linha:
    stream.seek(0);
    dentro do status do stream, após identificar que o status esta empty?

    Agradeço.

    Posted on julho 18th, 2009 at 4:07 pm

  2. web flv player:

    Thanks for given this great post…

    Posted on julho 29th, 2009 at 9:52 am

Leave a Reply