|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="zh-cmn-Hans-CN"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> |
| 6 | + <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script> |
| 7 | + <script src="https://cdn.staticfile.org/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script> |
| 8 | + <link rel="stylesheet" href="https://bootswatch.com/_vendor/bootstrap/dist/css/bootstrap.min.css" /> |
| 9 | + <link id="css" data-theme="bootstrap" rel="stylesheet" crossorigin="anonymous" /> |
| 10 | + <link rel="stylesheet" href="/basis/css/bootstrap-init.css" /> |
| 11 | + <style> |
| 12 | + audio#audio { |
| 13 | + width: inherit; |
| 14 | + } |
| 15 | + |
| 16 | + .range-form-control { |
| 17 | + display: flex; |
| 18 | + } |
| 19 | + |
| 20 | + .after-range-text { |
| 21 | + min-width: 4.2em; |
| 22 | + text-align: center; |
| 23 | + display: block; |
| 24 | + } |
| 25 | + |
| 26 | + * { |
| 27 | + user-select: none; |
| 28 | + } |
| 29 | + </style> |
| 30 | + </head> |
| 31 | + <body> |
| 32 | + <div class="container-fluid"> |
| 33 | + <title class="h4">音乐播放器</title> |
| 34 | + <meta name="discription" class="h6" content="一个非常简陋的音乐播放器" class="mb-3" /> |
| 35 | + <div class="row"> |
| 36 | + <div class="mb-2"> |
| 37 | + <input class="form-control" type="file" accept="audio/*,video/*,.mp3,.ogg,.flac,.wav,.ape,.aac,.m4a,.mp4,.mpg,.flv" id="music-file"> |
| 38 | + </div> |
| 39 | + <div class="mb-2"> |
| 40 | + <audio id="audio" controls loop></audio> |
| 41 | + </div> |
| 42 | + <div class="input-group mb-2"> |
| 43 | + <label class="input-group-text" for="volume-range">音量</label> |
| 44 | + <div class="form-control range-form-control"> |
| 45 | + <input type="range" class="form-range" id="volume-range" min="0" max="100" value="100" /> |
| 46 | + </div> |
| 47 | + <span class="input-group-text after-range-text" for="volume-range" id="volume-label">100%</span> |
| 48 | + </div> |
| 49 | + <div class="input-group mb-2"> |
| 50 | + <label class="input-group-text" for="speed-range">速度</label> |
| 51 | + <div class="form-control range-form-control"> |
| 52 | + <input type="range" class="form-range" id="speed-range" min="-200" max="200" value="0" step="4" /> |
| 53 | + </div> |
| 54 | + <span class="input-group-text after-range-text" for="speed-range" id="speed-label">1×</span> |
| 55 | + </div> |
| 56 | + <div class="btn-group mb-2" role="group" id="speed-group"> |
| 57 | + <button type="button" class="btn btn-outline-secondary">0.25×</button> |
| 58 | + <button type="button" class="btn btn-outline-secondary">0.5×</button> |
| 59 | + <button type="button" class="btn btn-outline-secondary">0.75×</button> |
| 60 | + <button type="button" class="btn btn-outline-secondary">1×</button> |
| 61 | + <button type="button" class="btn btn-outline-secondary">1.25×</button> |
| 62 | + <button type="button" class="btn btn-outline-secondary">1.5×</button> |
| 63 | + <button type="button" class="btn btn-outline-secondary">2×</button> |
| 64 | + <button type="button" class="btn btn-outline-secondary">3×</button> |
| 65 | + <button type="button" class="btn btn-outline-secondary">4×</button> |
| 66 | + </div> |
| 67 | + <div class="input-group mb-2"> |
| 68 | + <label class="input-group-text" for="preserves-pitch-check">速度保持音高</label> |
| 69 | + <div class="form-control" id="preserves-pitch-check-form-control"> |
| 70 | + <div class="form-check form-switch"> |
| 71 | + <input class="form-check-input" type="checkbox" role="switch" id="preserves-pitch-check" /> |
| 72 | + <label class="form-check-label" id="preserves-pitch-label">关</label> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + <div class="input-group mb-2"> |
| 77 | + <label class="input-group-text" for="loop-check">循环播放</label> |
| 78 | + <div class="form-control" id="loop-check-form-control"> |
| 79 | + <div class="form-check form-switch"> |
| 80 | + <input class="form-check-input" type="checkbox" role="switch" id="loop-check" checked /> |
| 81 | + <label class="form-check-label" id="loop-label">开</label> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + <script> |
| 88 | + ["preserves-pitch", "loop"].forEach(check => { |
| 89 | + $(`#${check}-check-form-control`).click(function (e) { |
| 90 | + if (e.target instanceof HTMLInputElement) return; |
| 91 | + const check = $(`#${check}-check`)[0]; |
| 92 | + check.checked = !check.checked; |
| 93 | + check.dispatchEvent(new Event("change")); |
| 94 | + check.focus(); |
| 95 | + }); |
| 96 | + }) |
| 97 | + |
| 98 | + $("[for]").click(function () { |
| 99 | + $("#" + $(this).attr("for")).focus(); |
| 100 | + }); |
| 101 | + |
| 102 | + const internalChange = { |
| 103 | + event: new Event("internalchange"), |
| 104 | + emit(el) { |
| 105 | + if (el instanceof jQuery) |
| 106 | + el = el[0]; |
| 107 | + el.dispatchEvent(this.event); |
| 108 | + }, |
| 109 | + is(event) { |
| 110 | + return event.type = this.event.type; |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + /** @type HTMLAudioElement */ |
| 115 | + const audio = $("#audio")[0]; |
| 116 | + audio.preservesPitch = false; |
| 117 | + |
| 118 | + $("#volume-range").bind("input propertychange change", function (e) { |
| 119 | + console.log(e); |
| 120 | + const volume = +this.value; |
| 121 | + $("#volume-label").text(volume + "%"); |
| 122 | + audio.volume = volume / 100; |
| 123 | + }); |
| 124 | + |
| 125 | + $("#speed-range").bind("input propertychange change", function (e) { |
| 126 | + const speed = 2 ** (+this.value / 100); |
| 127 | + audio.playbackRate = speed; |
| 128 | + }); |
| 129 | + |
| 130 | + $("#preserves-pitch-check").change(function () { |
| 131 | + const preserve = this.checked; |
| 132 | + $("#preserves-pitch-label").text(preserve ? "开" : "关"); |
| 133 | + audio.preservesPitch = preserve; |
| 134 | + }); |
| 135 | + |
| 136 | + $("#loop-check").change(function () { |
| 137 | + const loop = this.checked; |
| 138 | + $("#loop-label").text(loop ? "开" : "关"); |
| 139 | + audio.loop = loop; |
| 140 | + }); |
| 141 | + |
| 142 | + function removeTrailingZero(numberString) { |
| 143 | + return numberString.replace(/\.?0+$/, ""); |
| 144 | + } |
| 145 | + |
| 146 | + $("#speed-group button").click(function () { |
| 147 | + const speed = parseFloat(this.textContent); |
| 148 | + setPlaybackRate(speed); |
| 149 | + }); |
| 150 | + |
| 151 | + $(audio).on("volumechange", function () { |
| 152 | + const volume = this.volume * 100; |
| 153 | + $("#volume-range").val(volume).change(); |
| 154 | + }); |
| 155 | + |
| 156 | + $(audio).on("ratechange", function () { |
| 157 | + const speed = this.playbackRate; |
| 158 | + setPlaybackRate(speed); |
| 159 | + }); |
| 160 | + |
| 161 | + function setPlaybackRate(rate) { |
| 162 | + const rangeValue = Math.log2(rate) * 100; |
| 163 | + $("#speed-range").val(rangeValue); |
| 164 | + $("#speed-label").text(removeTrailingZero(rate.toFixed(2)) + "×"); |
| 165 | + audio.playbackRate = rate; |
| 166 | + } |
| 167 | + |
| 168 | + $("#music-file").change(function () { |
| 169 | + /** @type HTMLInputElement */ |
| 170 | + const thi = this; |
| 171 | + const file = thi.files[0]; |
| 172 | + if (!file) return; |
| 173 | + const blob = URL.createObjectURL(file); |
| 174 | + audio.src = blob; |
| 175 | + }); |
| 176 | + </script> |
| 177 | + <script src="/basis/NightTime.js"></script> |
| 178 | + </body> |
| 179 | +</html> |
0 commit comments