Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rudmin committed Jan 29, 2018
1 parent 72ed289 commit 221f1cf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 34 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ rec.onstop()

A callback which occurs when media recording ends.

```js
rec.onstreamerror( error )
```

A callback which occurs when a stream error occurs.


---------
### Gotchas
Expand Down
6 changes: 1 addition & 5 deletions dist-unminified/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ Recorder.prototype.setMonitorGain = function( gain ){
Recorder.prototype.start = function( sourceNode ){
if ( this.state === "inactive" ) {
var self = this;
this.state = "recording";
this.initAudioContext( sourceNode );
this.initAudioGraph();

return this.initSourceNode( sourceNode ).then( function( sourceNode ){
self.state = "recording";
self.encoder.postMessage( Object.assign({
command: 'init',
originalSampleRate: self.audioContext.sampleRate,
Expand All @@ -251,9 +251,6 @@ Recorder.prototype.start = function( sourceNode ){
self.sourceNode.connect( self.monitorNode );
self.sourceNode.connect( self.scriptProcessorNode );
self.onstart();
}, function( error ){
self.onstreamerror( error );
throw error;
});
}
};
Expand Down Expand Up @@ -310,7 +307,6 @@ Recorder.prototype.onpause = function(){};
Recorder.prototype.onresume = function(){};
Recorder.prototype.onstart = function(){};
Recorder.prototype.onstop = function(){};
Recorder.prototype.onstreamerror = function(){};


module.exports = Recorder;
Expand Down
2 changes: 1 addition & 1 deletion dist/recorder.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions example/encoder.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ <h2>Log</h2>
log.innerHTML += "\n" + text + " " + (data || '');
}

start.addEventListener( "click", function(){ recorder.start(); });
pause.addEventListener( "click", function(){ recorder.pause(); });
resume.addEventListener( "click", function(){ recorder.resume(); });
stopButton.addEventListener( "click", function(){ recorder.stop(); });

if (!Recorder.isRecordingSupported()) {
screenLogger("Recording features are not supported in your browser.");
}
Expand All @@ -74,6 +69,15 @@ <h2>Log</h2>
encoderPath: "../dist/encoderWorker.min.js"
});

pause.addEventListener( "click", function(){ recorder.pause(); });
resume.addEventListener( "click", function(){ recorder.resume(); });
stopButton.addEventListener( "click", function(){ recorder.stop(); });
start.addEventListener( "click", function(){
recorder.start().catch(function(e){
screenLogger('Error encountered: ' + e.message );
});
});

recorder.onstart = function(e){
screenLogger('Recorder is started');
start.disabled = resume.disabled = true;
Expand All @@ -98,10 +102,6 @@ <h2>Log</h2>
pause.disabled = stopButton.disabled = false;
};

recorder.onstreamerror = function(e){
screenLogger('Error encountered: ' + e.message );
};

recorder.ondataavailable = function( typedArray ){
var dataBlob = new Blob( [typedArray], { type: 'audio/ogg' } );
var fileName = new Date().toISOString() + ".opus";
Expand Down
14 changes: 9 additions & 5 deletions example/waveRecorder.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ <h2>Log</h2>
log.innerHTML += "\n" + text + " " + (data || '');
}

start.addEventListener( "click", function(){ recorder.start(); });
pause.addEventListener( "click", function(){ recorder.pause(); });
resume.addEventListener( "click", function(){ recorder.resume(); });
stopButton.addEventListener( "click", function(){ recorder.stop(); });

if (!Recorder.isRecordingSupported()) {
screenLogger("Recording features are not supported in your browser.");
}
Expand All @@ -68,6 +63,15 @@ <h2>Log</h2>
encoderPath: "../dist-unminified/waveWorker.js"
});

pause.addEventListener( "click", function(){ recorder.pause(); });
resume.addEventListener( "click", function(){ recorder.resume(); });
stopButton.addEventListener( "click", function(){ recorder.stop(); });
start.addEventListener( "click", function(){
recorder.start().catch(function(e){
screenLogger('Error encountered: ' + e.message );
});
});

recorder.onstart = function(){
screenLogger('Recorder is started');
start.disabled = resume.disabled = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opus-recorder",
"version": "3.0.1",
"version": "4.0.0",
"description": "A library for recording opus encoded audio",
"homepage": "https://github.com/chris-rudmin/opus-recorder",
"author": "Chris Rudmin",
Expand Down
6 changes: 1 addition & 5 deletions src/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ Recorder.prototype.setMonitorGain = function( gain ){
Recorder.prototype.start = function( sourceNode ){
if ( this.state === "inactive" ) {
var self = this;
this.state = "recording";
this.initAudioContext( sourceNode );
this.initAudioGraph();

return this.initSourceNode( sourceNode ).then( function( sourceNode ){
self.state = "recording";
self.encoder.postMessage( Object.assign({
command: 'init',
originalSampleRate: self.audioContext.sampleRate,
Expand All @@ -171,9 +171,6 @@ Recorder.prototype.start = function( sourceNode ){
self.sourceNode.connect( self.monitorNode );
self.sourceNode.connect( self.scriptProcessorNode );
self.onstart();
}, function( error ){
self.onstreamerror( error );
throw error;
});
}
};
Expand Down Expand Up @@ -230,7 +227,6 @@ Recorder.prototype.onpause = function(){};
Recorder.prototype.onresume = function(){};
Recorder.prototype.onstart = function(){};
Recorder.prototype.onstop = function(){};
Recorder.prototype.onstreamerror = function(){};


module.exports = Recorder;
3 changes: 1 addition & 2 deletions test/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('Recorder', function(){
sinon.spy(Recorder.prototype, 'onstop');
sinon.spy(Recorder.prototype, 'onpause');
sinon.spy(Recorder.prototype, 'onresume');
sinon.spy(Recorder.prototype, 'onstreamerror');
};

beforeEach(function(){
Expand Down Expand Up @@ -303,9 +302,9 @@ describe('Recorder', function(){
return rec.start().then( function(){
throw new Error('Unexpected promise resolving.');
}, function( ev ){
expect(rec.state).to.equal('inactive');
expect(ev).instanceof(Error);
expect(ev.message).to.equal('PermissionDeniedError')
expect(rec.onstreamerror).to.have.been.calledOnce;
});
});

Expand Down

0 comments on commit 221f1cf

Please sign in to comment.