Status.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.av.widget.Status"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.av.widget.Status"] = true;
  8. dojo.provide("dojox.av.widget.Status");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.declare("dojox.av.widget.Status", [dijit._Widget, dijit._Templated], {
  12. // summary:
  13. // A Status widget to use with dojox.av.widget.Player
  14. //
  15. // description:
  16. // Displays the name of the media file, and it's current status
  17. // (playing, paused, buffering, etc.) in the middle. Displays
  18. // the playhead time on the left and the duration on the right.
  19. //
  20. templateString: dojo.cache("dojox.av.widget", "resources/Status.html", "<table class=\"Status\">\n <tr>\n <td class=\"Time\"><span dojoAttachPoint=\"timeNode\">0.00</span></td>\n <td class=\"Status\"><div dojoAttachPoint=\"titleNode\">Loading...</div></td>\n <td class=\"Duration\"><span dojoAttachPoint=\"durNode\">0.00</span></td>\n </tr>\n</table>\n"),
  21. setMedia: function(/* Object */med){
  22. // summary:
  23. // A common method to set the media in all Player widgets.
  24. // May do connections and initializations.
  25. //
  26. this.media = med;
  27. dojo.connect(this.media, "onMetaData", this, function(data){
  28. this.duration = data.duration;
  29. this.durNode.innerHTML = this.toSeconds(this.duration);
  30. });
  31. dojo.connect(this.media, "onPosition", this, function(time){
  32. this.timeNode.innerHTML = this.toSeconds(time);
  33. });
  34. var cons = ["onMetaData", "onPosition", "onStart", "onBuffer", "onPlay", "onPaused", "onStop", "onEnd", "onError", "onLoad"];
  35. dojo.forEach(cons, function(c){
  36. dojo.connect(this.media, c, this, c);
  37. }, this);
  38. },
  39. onMetaData: function(data){
  40. this.duration = data.duration;
  41. this.durNode.innerHTML = this.toSeconds(this.duration);
  42. if(this.media.title){
  43. this.title = this.media.title;
  44. }else{
  45. var a = this.media.mediaUrl.split("/");
  46. var b = a[a.length-1].split(".")[0];
  47. this.title = b;
  48. }
  49. },
  50. onBuffer: function(isBuffering){
  51. this.isBuffering = isBuffering;
  52. console.warn("status onBuffer", this.isBuffering);
  53. if(this.isBuffering){
  54. this.setStatus("buffering...");
  55. }else{
  56. this.setStatus("Playing");
  57. }
  58. },
  59. onPosition:function(time){
  60. //console.log("onPosition:", time)
  61. // this.timeNode.innerHTML = this.toSeconds(time);
  62. },
  63. onStart: function(){
  64. this.setStatus("Starting");
  65. },
  66. onPlay: function(){
  67. this.setStatus("Playing");
  68. },
  69. onPaused: function(){
  70. this.setStatus("Paused");
  71. },
  72. onStop: function(){
  73. this.setStatus("Stopped");
  74. },
  75. onEnd: function(){
  76. this.setStatus("Stopped");
  77. },
  78. onError: function(evt){
  79. console.log("status error:", evt)
  80. var msg = evt.info.code;
  81. if(msg == "NetStream.Play.StreamNotFound"){
  82. msg = "Stream Not Found"
  83. }
  84. this.setStatus("ERROR: "+ msg, true);
  85. },
  86. onLoad: function(){
  87. this.setStatus("Loading...");
  88. },
  89. setStatus: function(str, isError){
  90. if(isError){
  91. dojo.addClass(this.titleNode, "statusError");
  92. }else{
  93. dojo.removeClass(this.titleNode, "statusError");
  94. if(this.isBuffering){
  95. str = "buffering...";
  96. }
  97. }
  98. //console.log(this.titleNode, "title:",this.title, "str:",str)
  99. this.titleNode.innerHTML = '<span class="statusTitle">'+this.title+'</span> <span class="statusInfo">'+str+'</span>';
  100. },
  101. toSeconds: function(time){
  102. var ts = time.toString()
  103. if(ts.indexOf(".")<0){
  104. ts += ".00"
  105. }else if(ts.length - ts.indexOf(".")==2){
  106. ts+="0"
  107. }else if(ts.length - ts.indexOf(".")>2){
  108. ts = ts.substring(0, ts.indexOf(".")+3)
  109. }
  110. return ts;
  111. }
  112. });
  113. }