-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make a recite fab button This change will add a recite fab button as the main action on the recite screen * Major context refactor * Fixing render errors * Remove console log
- Loading branch information
Showing
6 changed files
with
181 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import React from 'react'; | ||
|
||
export const AppContext = React.createContext({}); | ||
export const defaultValue = { | ||
name: "unknown", | ||
phrases: [], | ||
}; | ||
|
||
export const AppContext = React.createContext(defaultValue); | ||
|
||
export default AppContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React from 'react'; | ||
export const defaultValue = { | ||
state: null, | ||
audioUrl: null, | ||
recognition: null, | ||
setRecordingState: () => {}, | ||
record: () => { console.log("Not implemented!"); }, | ||
}; | ||
|
||
export const ReciteContext = React.createContext(defaultValue); | ||
|
||
export class DefaultReciteContextProvider extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
let mediaRecorder; | ||
|
||
const setRecordingState = (recordingState) => {; | ||
this.setState({ | ||
...this.state, | ||
state: recordingState, | ||
}); | ||
}; | ||
|
||
let setAudioUrl = (audioUrl) => { | ||
this.setState({ | ||
...this.state, | ||
audioUrl, | ||
}); | ||
}; | ||
|
||
let setRecognition = (recognition) => { | ||
this.setState((state) => ({ | ||
...this.state, | ||
recognition | ||
})); | ||
}; | ||
|
||
const record = ({ targetLanguageCode }) => { | ||
setRecognition("listening"); | ||
|
||
|
||
if(this.state.state !== "stopped") { | ||
let { recognition } = this.state; | ||
|
||
if(recognition) { | ||
recognition.abort(); | ||
} | ||
|
||
setRecordingState("stopped"); | ||
setRecognition(null); | ||
|
||
if(mediaRecorder) { | ||
mediaRecorder.stop(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if(navigator.mediaDevices) { | ||
let chunks = []; | ||
navigator.mediaDevices.getUserMedia({ audio: true }) | ||
.then(function(stream) { | ||
|
||
mediaRecorder = new MediaRecorder(stream); | ||
|
||
mediaRecorder.start(); | ||
|
||
mediaRecorder.onstart = function(e) { | ||
}; | ||
|
||
mediaRecorder.onstop = function(e) { | ||
setRecordingState("stopped"); | ||
|
||
var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' }); | ||
chunks = []; | ||
|
||
var audioURL = URL.createObjectURL(blob); | ||
|
||
var audio = new Audio(); | ||
audio.src = audioURL; | ||
audio.play(); | ||
|
||
setAudioUrl(audioURL); | ||
}; | ||
|
||
mediaRecorder.ondataavailable = function(e) { | ||
chunks.push(e.data); | ||
} | ||
}); | ||
} | ||
|
||
const recognition = new window.webkitSpeechRecognition(); | ||
recognition.continuous = false; | ||
recognition.interimResults = false; | ||
recognition.lang = targetLanguageCode; | ||
|
||
recognition.addEventListener('audiostart', () => { | ||
setRecordingState("listening"); | ||
}); | ||
|
||
recognition.addEventListener('soundstart', (e) => { | ||
setRecordingState("recording"); | ||
}); | ||
|
||
recognition.addEventListener('soundend', (e) => { | ||
recognition.stop(); | ||
|
||
if(mediaRecorder) { | ||
mediaRecorder.stop(); | ||
} | ||
|
||
|
||
setRecognition(null); | ||
}); | ||
|
||
recognition.start(); | ||
|
||
setRecognition("listening"); | ||
|
||
setRecognition(recognition); | ||
|
||
}; | ||
|
||
this.state = { | ||
...defaultValue, | ||
state: "stopped", | ||
record, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<ReciteContext.Provider value={this.state}> | ||
{ this.props.children } | ||
</ReciteContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
export default ReciteContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters