1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/dom-construct", |
---|
4 | "dojo/sniff", |
---|
5 | "dijit/_Contained", |
---|
6 | "dijit/_WidgetBase" |
---|
7 | ], function(declare, domConstruct, has, Contained, WidgetBase){ |
---|
8 | // module: |
---|
9 | // dojox/mobile/Audio |
---|
10 | |
---|
11 | return declare("dojox.mobile.Audio", [WidgetBase, Contained], { |
---|
12 | // summary: |
---|
13 | // A thin wrapper around the HTML5 `<audio>` element. |
---|
14 | // description: |
---|
15 | // dojox/mobile/Audio is a widget which plays audio. If all sources cannot |
---|
16 | // be played (typically, in desktop browsers that do not support `<audio>`), |
---|
17 | // dojox/mobile/Audio automatically replaces `<audio>` with `<embed>`, such |
---|
18 | // that the browser tries to play it with a suitable plug-in. |
---|
19 | |
---|
20 | // source: [const] Array |
---|
21 | // An array of src and type, |
---|
22 | // ex. [{src:"a.mp3", type:"audio/mpeg"}, {src:"a.ogg", type:"audio/ogg"}, ...]. |
---|
23 | // The src gives the path of the media resource. The type gives the |
---|
24 | // type of the media resource. |
---|
25 | // Note that changing the value of the property after the widget |
---|
26 | // creation has no effect. |
---|
27 | source: null, |
---|
28 | |
---|
29 | // width: [const] String |
---|
30 | // The width of the embed element. |
---|
31 | // Note that changing the value of the property after the widget |
---|
32 | // creation has no effect. |
---|
33 | width: "200px", |
---|
34 | |
---|
35 | // height: [const] String |
---|
36 | // The height of the embed element. |
---|
37 | // Note that changing the value of the property after the widget |
---|
38 | // creation has no effect. |
---|
39 | height: "15px", |
---|
40 | |
---|
41 | // _playable: [private] Boolean |
---|
42 | // Internal flag. |
---|
43 | _playable: false, |
---|
44 | |
---|
45 | // _tag: [private] String |
---|
46 | // The name of the tag ("audio"). |
---|
47 | _tag: "audio", |
---|
48 | |
---|
49 | constructor: function(){ |
---|
50 | // summary: |
---|
51 | // Creates a new instance of the class. |
---|
52 | this.source = []; |
---|
53 | }, |
---|
54 | |
---|
55 | buildRendering: function(){ |
---|
56 | this.domNode = this.srcNodeRef || domConstruct.create(this._tag); |
---|
57 | }, |
---|
58 | |
---|
59 | _getEmbedRegExp: function(){ |
---|
60 | // tags: |
---|
61 | // private |
---|
62 | return has('ff') ? /audio\/mpeg/i : |
---|
63 | has('ie') ? /audio\/wav/i : |
---|
64 | null; |
---|
65 | }, |
---|
66 | |
---|
67 | startup: function(){ |
---|
68 | if(this._started){ return; } |
---|
69 | this.inherited(arguments); |
---|
70 | var i; |
---|
71 | if(this.domNode.canPlayType){ |
---|
72 | if(this.source.length > 0){ |
---|
73 | for(i = 0, len = this.source.length; i < len; i++){ |
---|
74 | domConstruct.create("source", {src:this.source[i].src, type:this.source[i].type}, this.domNode); |
---|
75 | this._playable = this._playable || !!this.domNode.canPlayType(this.source[i].type); |
---|
76 | } |
---|
77 | }else{ |
---|
78 | for(i = 0, len = this.domNode.childNodes.length; i < len; i++){ |
---|
79 | var n = this.domNode.childNodes[i]; |
---|
80 | if(n.nodeType === 1 && n.nodeName === "SOURCE"){ |
---|
81 | this.source.push({src:n.src, type:n.type}); |
---|
82 | this._playable = this._playable || !!this.domNode.canPlayType(n.type); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | has.add("mobile-embed-audio-video-support", true); //It should move to staticHasFeatures |
---|
88 | if(has("mobile-embed-audio-video-support")){ |
---|
89 | if(!this._playable){ |
---|
90 | for(i = 0, len = this.source.length, re = this._getEmbedRegExp(); i < len; i++){ |
---|
91 | if(this.source[i].type.match(re)){ |
---|
92 | var node = domConstruct.create("embed", { |
---|
93 | src: this.source[0].src, |
---|
94 | type: this.source[0].type, |
---|
95 | width: this.width, |
---|
96 | height: this.height |
---|
97 | }); |
---|
98 | this.domNode.parentNode.replaceChild(node, this.domNode); |
---|
99 | this.domNode = node; |
---|
100 | this._playable = true; |
---|
101 | break; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | }); |
---|
109 | }); |
---|