Переглянути джерело

Added THREE.AudioContext.

Mr.doob 9 роки тому
батько
коміт
e207eb57ca

+ 2 - 3
examples/misc_sound.html

@@ -78,9 +78,6 @@
 				var listener = new THREE.AudioListener();
 				camera.add( listener );
 
-				var audioLoader = new THREE.AudioLoader(listener.context);
-
-
 				scene = new THREE.Scene();
 				scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );
 
@@ -96,6 +93,8 @@
 
 				// sound spheres
 
+				var audioLoader = new THREE.AudioLoader();
+
 				var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
 				mesh1.position.set( -250, 30, 0 );
 				scene.add( mesh1 );

+ 4 - 2
src/audio/Audio.js

@@ -41,12 +41,14 @@ THREE.Audio.prototype.load = function ( file ) {
 
 	console.warn( 'THREE.Audio: .load has been deprecated. Please use THREE.AudioLoader.' );
 
-	var audioLoader = new THREE.AudioLoader( this.context );
 	var scope = this;
 
+	var audioLoader = new THREE.AudioLoader();
 	audioLoader.load( file, function ( buffer ) {
+
 		scope.setBuffer( buffer );
-	});
+
+	} );
 
 	return this;
 

+ 25 - 0
src/audio/AudioContext.js

@@ -0,0 +1,25 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+Object.defineProperty( THREE, 'AudioContext', {
+
+	get: ( function () {
+
+		var context;
+
+		return function () {
+
+			if ( context === undefined ) {
+
+				context = new ( window.AudioContext || window.webkitAudioContext )();
+
+			}
+
+			return context;
+
+		};
+
+	} )()
+
+} );

+ 1 - 1
src/audio/AudioListener.js

@@ -8,7 +8,7 @@ THREE.AudioListener = function () {
 
 	this.type = 'AudioListener';
 
-	this.context = new ( window.AudioContext || window.webkitAudioContext )();
+	this.context = THREE.AudioContext;
 
 	this.gain = this.context.createGain();
 	this.gain.connect( this.context.destination );

+ 4 - 6
src/loaders/AudioLoader.js

@@ -2,9 +2,7 @@
  * @author Reece Aaron Lecrivain / http://reecenotes.com/
  */
 
-THREE.AudioLoader = function ( context, manager ) {
-
-	this.context = ( context !== undefined ) ? context : new ( window.AudioContext || window.webkitAudioContext )();
+THREE.AudioLoader = function ( manager ) {
 
 	this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
 
@@ -16,13 +14,13 @@ THREE.AudioLoader.prototype = {
 
 	load: function ( url, onLoad, onProgress, onError ) {
 
-		var scope = this;
-
 		var loader = new THREE.XHRLoader( this.manager );
 		loader.setResponseType( 'arraybuffer' );
 		loader.load( url, function ( buffer ) {
 
-			scope.context.decodeAudioData( buffer, function ( audioBuffer ) {
+			var context = THREE.AudioContext;
+
+			context.decodeAudioData( buffer, function ( audioBuffer ) {
 
 				onLoad( audioBuffer );
 

+ 1 - 0
utils/build/includes/common.json

@@ -54,6 +54,7 @@
 	"src/animation/tracks/VectorKeyframeTrack.js",
 	"src/audio/Audio.js",
 	"src/audio/AudioAnalyser.js",
+	"src/audio/AudioContext.js",
 	"src/audio/PositionalAudio.js",
 	"src/audio/AudioListener.js",
 	"src/cameras/Camera.js",