10000 add beep on iOS by maslick · Pull Request #37 · maslick/koder · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add beep on iOS #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions docs/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
////////////////////////
// Fix iOS AudioContext
////////////////////////
(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);
})();

////////////////
// Worker
////////////////
Expand Down Expand Up @@ -115,23 +150,23 @@ function stopVideo() {
init();
}

function beep(freq = 750, duration = 150, vol = 5) {
const AudioContext = window.AudioContext || window.webkitAudioContext || false;
if (!AudioContext) {
console.warn("Sorry, but the Web Audio API is not supported by your browser");
return;
const beep = (freq = 750, duration = 150, vol = 5) => {
try {
const context = window.audioContext;
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.connect(gain);
oscillator.frequency.value = freq;
oscillator.type = "square";
gain.connect(context.destination);
gain.gain.value = vol * 0.01;
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + duration * 0.001);
} catch (e) {
console.warn("Sorry, Web Audio API is not supported by your browser");
console.warn(e.toString());
}
const context = new AudioContext();
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.connect(gain);
oscillator.frequency.value = freq;
oscillator.type = "square";
gain.connect(context.destination);
gain.gain.value = vol * 0.01;
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + duration * 0.001);
}
};

////////////////
// DOM
Expand Down < 10000 /td>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koder-react",
"version": "1.2.7",
"version": "1.3.0",
"homepage": "./",
"private": true,
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@
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
26 changes: 13 additions & 13 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const beep = (freq = 750, duration = 150, vol = 5) => {
const AudioContext = window.AudioContext || window.webkitAudioContext || false;
if (!AudioContext) {
try {
const context = window.audioContext;
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.connect(gain);
oscillator.frequency.value = freq;
oscillator.type = "square";
gain.connect(context.destination);
gain.gain.value = vol * 0.01;
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + duration * 0.001);
} catch (e) {
console.warn("Sorry, Web Audio API is not supported by your browser");
return;
console.warn(e.toString());
}
const context = new AudioContext();
const oscillator = context.createOscillator();
const gain = context.createGain();
oscillator.connect(gain);
oscillator.frequency.value = freq;
oscillator.type = "square";
gain.connect(context.destination);
gain.gain.value = vol * 0.01;
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + duration * 0.001);
};

const WORKER_TYPE = {
Expand Down
0