浏览代码

fixes for audio api not present in JS (close #220)

Nicolas Cannasse 8 年之前
父节点
当前提交
c71494272a
共有 2 个文件被更改,包括 5 次插入1 次删除
  1. 1 0
      hxd/snd/Mp3Data.hx
  2. 4 1
      hxd/snd/NativeChannel.hx

+ 1 - 0
hxd/snd/Mp3Data.hx

@@ -49,6 +49,7 @@ class Mp3Data extends Data {
 		#elseif js
 
 		var ctx = @:privateAccess NativeChannel.getContext();
+		if( ctx == null ) return;
 		ctx.decodeAudioData(bytes.getData(), processBuffer);
 
 		var decodedRate = Std.int(ctx.sampleRate);

+ 4 - 1
hxd/snd/NativeChannel.hx

@@ -108,8 +108,10 @@ class NativeChannel {
 		if( ctx == null ) {
 			try {
 				ctx = new js.html.audio.AudioContext();
+			} catch( e : Dynamic ) try {
+				ctx = untyped __js__('new window.webkitAudioContext()');
 			} catch( e : Dynamic ) {
-				throw "Web Audio API not available for this browser";
+				ctx = null;
 			}
 		}
 		return ctx;
@@ -131,6 +133,7 @@ class NativeChannel {
 		channel = snd.play(0, 0x7FFFFFFF);
 		#elseif js
 		var ctx = getContext();
+		if( ctx == null ) return;
 		sproc = ctx.createScriptProcessor(bufferSamples, 2, 2);
 		tmpBuffer = new haxe.io.Float32Array(bufferSamples * 2);
 		sproc.connect(ctx.destination);