Skip to content

Commit

Permalink
Merge pull request #51 from maslick/fix-issue-50-beep
Browse files Browse the repository at this point in the history
fix audio beep
  • Loading branch information
maslick authored Jan 19, 2022
2 parents 80e8e2e + 959e654 commit fbf9c6e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
32 changes: 0 additions & 32 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,6 @@
trackLinks:true,
accurateTrackBounce:true
});

(function() {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
if (window.AudioContext) {
window.audioContext = new window.AudioContext();
}
const fixAudioContext = function (e) {
if (window.audioContext) {
// Create empty buffer
const buffer = window.audioContext.createBuffer(1, 1, 22050);
const source = window.audioContext.createBufferSource();
source.buffer = buffer;
// Connect to output (speakers)
source.connect(window.audioContext.destination);
// Play sound
if (source.start) {
source.start(0);
} else if (source.play) {
source.play(0);
} else if (source.noteOn) {
source.noteOn(0);
}
}
// Remove events
document.removeEventListener('touchstart', fixAudioContext);
document.removeEventListener('touchend', fixAudioContext);
};
// iOS 6-8
document.addEventListener('touchstart', fixAudioContext);
// iOS 9
document.addEventListener('touchend', fixAudioContext);
})();
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/57312736" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
</head>
Expand Down
34 changes: 33 additions & 1 deletion src/components/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ class Scan extends React.Component {
});
};

initializeAudio = () => {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
if (window.AudioContext) {
window.audioContext = new window.AudioContext();
}
const fixAudioContext = function (e) {
if (window.audioContext) {
// Create empty buffer
const buffer = window.audioContext.createBuffer(1, 1, 22050);
const source = window.audioContext.createBufferSource();
source.buffer = buffer;
// Connect to output (speakers)
source.connect(window.audioContext.destination);
// Play sound
if (source.start) {
source.start(0);
} else if (source.play) {
source.play(0);
} else if (source.noteOn) {
source.noteOn(0);
}
}
// Remove events
document.removeEventListener('touchstart', fixAudioContext);
document.removeEventListener('touchend', fixAudioContext);
};
// iOS 6-8
document.addEventListener('touchstart', fixAudioContext);
// iOS 9
document.addEventListener('touchend', fixAudioContext);
}

stopScan = () => {
this.setState({
scanning: false,
Expand Down Expand Up @@ -324,7 +356,7 @@ class Scan extends React.Component {

renderButtons = () => {
return <div className="scanBtn">
<a href="!#" className="myHref" onClick={this.onBtnClickHandler} style={this.startStyle()}>{this.state.btnText}</a>
<a href="!#" className="myHref" onTouchStart={this.initializeAudio} onClick={this.onBtnClickHandler} style={this.startStyle()}>{this.state.btnText}</a>
<a href="!#" className="myHref" onClick={this.onCrossHairClickHandler} style={this.xHairStyle()}>X-hair</a>
<a href="!#" className="myHref" onClick={this.onFPSClickHandler} style={this.fpsStyle()}>FPS</a>
<a href="!#" className="myHref" onClick={this.onBWClickHandler} style={this.bwStyle()}>B/W</a>
Expand Down

0 comments on commit fbf9c6e

Please sign in to comment.