基于uniapp+vue3开发的音频播放器,支持H5
2022年7月19日
播放器预览图

实现的功能
- 在线播放指定音频文件、可暂停、快进
- 支持显示标题和播放时长
代码内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
<template> <div class="player"> <div class="player-controls"> <a v-if="showSkip && !livestream" class="player-back-15-icon" aria-label="go back 15 seconds" @click="goBack15" > <back15-icon/> </a> <a class="player-play-pause-icon" :aria-label="state.playing ? 'pause' : 'play'" @click="togglePlay" > <play-icon v-if="!state.playing"/> <pause-icon v-if="state.playing"/> </a> <a v-if="showSkip && !livestream" class="player-ahead-15-icon" aria-label="go ahead 15 seconds" @click="goAhead15" > <ahead15-icon/> </a> <div class="player-track"> <div class="player-track-title"> <span v-if="hasTitle">{{ title }}</span> <span v-if="hasTitle && hasDetails"> - </span> <span v-if="hasDetails && !hasDetailsLink" class="player-track-title-details" > {{ details }} </span> <a v-if="hasDetails && hasDetailsLink" :href="detailsLink" class="player-track-title-details-link" > {{ details }} </a> </div> <template v-if="!livestream"> <div class="player-track-progress" @click.prevent="seek" > <div :style="{ width: percentComplete + '%' }" class="player-track-seeker" /> <div :style="{ width: percentBuffered + '%' }" class="player-track-buffered" /> </div> <div class="player-track-time"> <span class="player-track-time-current">{{ convertTimeHHMMSS(state.currentSeconds) }}</span> <span class="player-track-time-separator">/</span> <span class="player-track-time-total">{{ convertTimeHHMMSS(state.durationSeconds) }}</span> </div> </template> </div> <div class="player-volume" @mouseover.prevent="state.showVolume = true" @mouseleave.prevent="state.showVolume = false" > <label for="playerVolume" class="hide-ally-element" > volume slider </label> <transition name="slide-left"> <input v-show="state.showVolume" id="playerVolume" v-model="state.volume" type="range" min="0" max="100" > </transition> <a tabindex="0" class="player-volume-icon" :aria-label="muted ? 'unmute' : 'mute'" @click="mute" @keypress.space.enter="mute" > <volume-icon v-if="!muted"/> <volume-muted-icon v-if="muted"/> </a> </div> <a v-if="showDownload && !livestream" tabindex="0" class="player-download-icon" aria-label="download" @click="download" @keypress.space.enter="download" > <download-icon/> </a> </div> <!-- ifdef H5--> <!-- <audio ref="audioFile" :loop="innerLoop" :src="file" preload="auto" style="display: none" />--> <!-- endif--> </div> </template> <script setup> import PlayIcon from './svg/PlayIcon' import PauseIcon from './svg/PauseIcon' import Back15Icon from './svg/Back15Icon' import Ahead15Icon from './svg/Ahead15Icon' import VolumeIcon from './svg/VolumeIcon' import VolumeMutedIcon from './svg/VolumeMutedIcon' import DownloadIcon from './svg/DownloadIcon' import {computed, onMounted, reactive, watch, defineProps, onBeforeUnmount} from "vue"; import log from '@/utils/logs' const convertTimeHHMMSS = (value) => { const hhmmss = new Date(value * 1000).toISOString().substr(11, 8) return hhmmss.indexOf('00:') === 0 ? hhmmss.substr(3) : hhmmss } const props = defineProps({ autoPlay: { type: Boolean, default: false }, details: { type: String, default: null }, detailsLink: { type: String, default: null }, file: { type: String, default: null }, livestream: { type: Boolean, default: false }, loop: { type: Boolean, default: false }, showDownload: { type: Boolean, default: false }, showSkip: { type: Boolean, default: true }, showTrack: { type: Boolean, default: true }, title: { type: String, default: null }, titleLink: { type: String, default: null } }) const state = reactive({ audio: undefined, currentSeconds: 0, durationSeconds: 0, buffered: 0, innerLoop: props.loop, loaded: false, playing: false, previousVolume: 35, showVolume: false, volume: 100, }) const hasDetails = computed(() => { return props.details }) const hasDetailsLink = computed(() => { return props.detailsLink }) const hasTitle = computed(() => { return props.title }) const muted = computed(() => { return state.volume / 100 === 0 }) const percentBuffered = computed(() => { return (state.buffered / state.durationSeconds) * 100 }) const percentComplete = computed(() => { return (state.currentSeconds / state.durationSeconds) * 100 }) watch(()=>state.playing, (newValue) => { log.info("watch state.playing",newValue) if (newValue) { return state.audio.play() } state.audio.pause() }) watch(()=>state.volume, () => { state.audio.volume = state.volume / 100 }) // keyboard accessibility window.addEventListener('keydown', event => { switch (event.code) { case 'Space': togglePlay() break case 'Enter': togglePlay() break case 'ArrowUp': if (state.volume < 100) state.volume++ break case 'ArrowDown': if (state.volume > 0) state.volume-- break case 'ArrowLeft': goBack15() break case 'ArrowRight': goAhead15() break } }) onMounted(() => { let innerAudioContext = uni.createInnerAudioContext(); innerAudioContext.autoplay = props.autoPlay; innerAudioContext.src = props.file; innerAudioContext.loop = props.loop; log.info("要播放的文件地址:",props.file) innerAudioContext.onPlay(() => { state.playing = true log.info("开始播放",props.file) }); innerAudioContext.onTimeUpdate(update); innerAudioContext.onEnded(function () { stop() }); innerAudioContext.onPause(() => { state.playing = false }) innerAudioContext.onError((error) => { log.info("播放异常",error) }); innerAudioContext.onCanplay(()=>{ state.durationSeconds = innerAudioContext.duration }) state.audio = innerAudioContext }) onBeforeUnmount(()=>{ if(state.audio!=null){ state.audio.destroy() } }) const download = () => { stop() window.open(this.file, 'download') } const goAhead15 = () => { state.audio.currentTime = state.audio.currentTime + 15 } const goBack15 = () => { state.audio.currentTime = state.audio.currentTime - 15 } const load = () => { if (state.audio.readyState >= 2) { state.loaded = true state.durationSeconds = parseInt(state.audio.duration) state.playing = props.autoPlay return state.playing } throw new Error('Failed to load sound file.') } const mute = () => { if (muted.value) { state.volume = state.previousVolume return state.volume } state.previousVolume = state.volume state.volume = 0 } const seek = (e) => { if (!state.loaded) return const el = e.target.getBoundingClientRect() const seekPos = (e.clientX - el.left) / el.width state.audio.currentTime = (state.audio.duration * seekPos) } const stop = () => { state.playing = false state.audio.currentTime = 0 } const togglePlay = () => { state.playing = !state.playing } const update = () => { state.currentSeconds = state.audio.currentTime state.buffered = state.audio.buffered } </script> <style lang="scss" scoped> $breakpoint: 768px; $--player-background: #005d8d; $--player-font-size: .9rem; $--player-font-size-small: .7rem; $--player-font-weight: 300; $--player-font-weight-bold: 600; $--player-text-color: #ffffff; $--player-icon-color: $--player-text-color; $--player-link-color: $--player-text-color; $--player-progress-color: #005d8d; $--player-buffered-color: rgba(0, 99, 141, 0.68); $--player-seeker-color: #ffffff; $--player-input-range-color: $--player-text-color; .player { width: 100%; background-color: $--player-background; padding: .85rem; font-weight: $--player-font-weight; position: relative; } .player a, .player a:visited, .player a:active { color: $--player-link-color; text-decoration: none; &:hover { text-decoration: underline; } } .player-controls { display: flex; align-items: center; } .player-back-15-icon, .player-play-pause-icon, .player-ahead-15-icon, .player-download-icon, .player-volume-icon { display: flex; fill: $--player-icon-color; } .player-back-15-icon { margin-right: 1rem; width: 20px; } .player-ahead-15-icon { margin-left: 1rem; width: 20px; } .player-download-icon { display: none; @media all and (min-width: $breakpoint) { display: flex; margin-left: 1rem; } } .player-track { flex: auto; padding: 0 2rem; overflow: hidden; } .player-track-title { color: $--player-text-color; font-size: $--player-font-size; font-weight: $--player-font-weight-bold; width: 100%; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; } .player-track-title-details { color: $--player-text-color; font-weight: 300; } .player-track-progress { position: absolute; background-color: $--player-progress-color; cursor: pointer; min-width: 200px; top: 0; left: 0; right: 0; height: 5px; @media all and (min-width: $breakpoint) { top: -5px; height: 3px; margin-top: .75rem; position: relative; } } .player-track-progress .player-track-seeker { background-color: $--player-seeker-color; bottom: 0; left: 0; position: absolute; top: 0; z-index: 20; } .player-track-progress .player-track-buffered { background-color: $--player-buffered-color; bottom: 0; left: 0; position: absolute; top: 0; z-index: 10; } .player-track-progress .player-track-playhead { position: absolute; height: 22px; width: 22px; margin: -8px -16px; transform: scale(0, 0); left: 0; opacity: 0; bottom: 0; transition: opacity .2s linear, transform .2s; &::after { content: ''; height: 22px; width: 22px; background-color: $--player-buffered-color; border-radius: 50%; opacity: 1; display: block; position: absolute; left: calc(50% - 11px); top: calc(50% - 11px); } } .player-track-time { display: flex; color: $--player-text-color; font-size: $--player-font-size-small; font-weight: $--player-font-weight-bold; @media all and (min-width: $breakpoint) { justify-content: flex-end; } } .player-track-time .player-track-time-current { opacity: 1; margin-right: .25rem; } .player-track-time .player-track-time-separator { opacity: .6; } .player-track-time .player-track-time-total { opacity: .6; margin-left: .25rem; } .player-volume { display: none; @media all and (min-width: $breakpoint) { display: flex; justify-content: flex-end; } } // input range base styles input[type=range] { -webkit-appearance: none; /* Hides the slider so that custom slider can be made */ background: transparent; /* Otherwise white in Chrome */ width: 100%; /* Chrome needs this */ margin-right: 1.5rem; } input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; } input[type=range]:focus { outline: none; /* Removes the blue border. You should probably do some kind of focus styling for accessibility reasons though. */ } input[type=range]::-ms-track { width: 100%; cursor: pointer; // Hides the slider so custom styles can be added background: transparent; border-color: transparent; color: transparent; } // input range thumb input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; height: 15px; width: 15px; border-radius: 50%; background: $--player-input-range-color; cursor: pointer; margin-top: -6px; /* (thumb height/2 + track height/2) You need to specify a margin in Chrome, but in Firefox and IE it is automatic */ } input[type=range]::-moz-range-thumb { height: 15px; width: 15px; border-radius: 50%; background: $--player-input-range-color; cursor: pointer; } input[type=range]::-ms-thumb { height: 15px; width: 15px; border-radius: 50%; background: $--player-input-range-color; cursor: pointer; } // input range track input[type=range]::-webkit-slider-runnable-track { width: 100%; height: 3px; cursor: pointer; background: $--player-input-range-color; } input[type=range]:focus::-webkit-slider-runnable-track { background: $--player-input-range-color; } input[type=range]::-moz-range-track { width: 100%; height: 3px; cursor: pointer; background: $--player-input-range-color; } input[type=range]::-ms-track { width: 100%; height: 3px; cursor: pointer; background: transparent; border-color: transparent; border-width: 16px 0; color: transparent; } input[type=range]::-ms-fill-lower { background: $--player-input-range-color; } input[type=range]:focus::-ms-fill-lower { background: $--player-input-range-color; } input[type=range]::-ms-fill-upper { background: $--player-input-range-color; } input[type=range]:focus::-ms-fill-upper { background: $--player-input-range-color; } // slide in transition .slide-left-enter-active, .slide-left-leave-active, .slide-right-enter-active, .slide-right-leave-active { transition-duration: 0.5s; transition-property: height, opacity, transform; transition-timing-function: cubic-bezier(0.55, 0, 0.1, 1); overflow: hidden; } .slide-left-enter, .slide-right-leave-active { opacity: 0; transform: translate(2em, 0); } .slide-left-leave-active, .slide-right-enter { opacity: 0; transform: translate(-2em, 0); } </style> |
代码当中涉及到几个图标文件
1 |
Ahead15Icon.vue |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<template> <svg height="32" viewBox="0 0 32 32" width="32" xmlns="http://www.w3.org/2000/svg" class="ahead-15-icon" role="img" > <title>Go Ahead 15 Seconds</title> <path d="m0 18c0 4.779 2.095 9.068 5.417 12l2.646-3c-2.491-2.199-4.063-5.416-4.063-9 0-6.627 5.373-12 12-12 3.314 0 6.314 1.343 8.485 3.515l-4.485 4.485h12v-12l-4.687 4.687c-2.895-2.896-6.895-4.687-11.313-4.687-8.837 0-16 7.163-16 16z" /> </svg> </template> |
Back15Icon.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<template> <svg height="32" viewBox="0 0 32 32" width="32" xmlns="http://www.w3.org/2000/svg" class="back-15-icon" role="img" > <title>Go Back 15 Seconds</title> <path d="m16 2c-4.418 0-8.418 1.791-11.313 4.687l-4.686-4.687v12h12l-4.485-4.485c2.172-2.172 5.172-3.515 8.485-3.515 6.627 0 12 5.373 12 12 0 3.584-1.572 6.801-4.063 9l2.646 3c3.322-2.932 5.417-7.221 5.417-12 0-8.837-7.163-16-16-16z" /> </svg> </template> |
DownloadIcon.vue