|
@@ -2,7 +2,7 @@ function PCMPlayer(option) {
|
|
|
this.init(option);
|
|
|
}
|
|
|
|
|
|
-PCMPlayer.prototype.init = function(option) {
|
|
|
+PCMPlayer.prototype.init = function (option) {
|
|
|
var defaults = {
|
|
|
encoding: '16bitInt',
|
|
|
channels: 1,
|
|
@@ -40,19 +40,28 @@ PCMPlayer.prototype.getTypedArray = function () {
|
|
|
return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.createContext = function() {
|
|
|
+PCMPlayer.prototype.createContext = function () {
|
|
|
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
- this.gainNode = this.audioCtx.createGain();
|
|
|
- this.gainNode.gain.value = 1;
|
|
|
- this.gainNode.connect(this.audioCtx.destination);
|
|
|
- this.startTime = this.audioCtx.currentTime;
|
|
|
+ $.alert({
|
|
|
+ title: '提示',
|
|
|
+ text: '开始使用云手机',
|
|
|
+ onOK: () => {
|
|
|
+ this.audioCtx.resume().then(() => {
|
|
|
+ this.gainNode = this.audioCtx.createGain();
|
|
|
+ this.gainNode.gain.value = 1;
|
|
|
+ this.gainNode.connect(this.audioCtx.destination);
|
|
|
+ this.startTime = this.audioCtx.currentTime;
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.isTypedArray = function(data) {
|
|
|
+PCMPlayer.prototype.isTypedArray = function (data) {
|
|
|
return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.feed = function(data) {
|
|
|
+PCMPlayer.prototype.feed = function (data) {
|
|
|
if (!this.isTypedArray(data)) return;
|
|
|
data = this.getFormatedValue(data);
|
|
|
var tmp = new Float32Array(this.samples.length + data.length);
|
|
@@ -61,7 +70,7 @@ PCMPlayer.prototype.feed = function(data) {
|
|
|
this.samples = tmp;
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.getFormatedValue = function(data) {
|
|
|
+PCMPlayer.prototype.getFormatedValue = function (data) {
|
|
|
var data = new this.typedArray(data.buffer),
|
|
|
float32 = new Float32Array(data.length),
|
|
|
i;
|
|
@@ -72,11 +81,11 @@ PCMPlayer.prototype.getFormatedValue = function(data) {
|
|
|
return float32;
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.volume = function(volume) {
|
|
|
+PCMPlayer.prototype.volume = function (volume) {
|
|
|
this.gainNode.gain.value = volume;
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.destroy = function() {
|
|
|
+PCMPlayer.prototype.destroy = function () {
|
|
|
if (this.interval) {
|
|
|
clearInterval(this.interval);
|
|
|
}
|
|
@@ -85,7 +94,7 @@ PCMPlayer.prototype.destroy = function() {
|
|
|
this.audioCtx = null;
|
|
|
};
|
|
|
|
|
|
-PCMPlayer.prototype.flush = function() {
|
|
|
+PCMPlayer.prototype.flush = function () {
|
|
|
if (!this.samples.length) return;
|
|
|
var bufferSource = this.audioCtx.createBufferSource(),
|
|
|
length = this.samples.length / this.option.channels,
|
|
@@ -104,16 +113,16 @@ PCMPlayer.prototype.flush = function() {
|
|
|
audioData[i] = this.samples[offset];
|
|
|
/* fadein */
|
|
|
if (i < 50) {
|
|
|
- audioData[i] = (audioData[i] * i) / 50;
|
|
|
+ audioData[i] = (audioData[i] * i) / 50;
|
|
|
}
|
|
|
/* fadeout*/
|
|
|
if (i >= (length - 51)) {
|
|
|
- audioData[i] = (audioData[i] * decrement--) / 50;
|
|
|
+ audioData[i] = (audioData[i] * decrement--) / 50;
|
|
|
}
|
|
|
offset += this.option.channels;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (this.startTime < this.audioCtx.currentTime) {
|
|
|
this.startTime = this.audioCtx.currentTime;
|
|
|
}
|