vue3中实现音频播放器APlayer
生活随笔
收集整理的這篇文章主要介紹了
vue3中实现音频播放器APlayer
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言:
????????vue2的時(shí)候,分享了一個(gè)很好用的插件是vue-aplayer,但是他是不支持vue3的,這里分享vue3使用APlayer來實(shí)現(xiàn)一個(gè)播放器的方法。
實(shí)現(xiàn)效果:
官方:
????????git地址:點(diǎn)我
????????api地址:點(diǎn)我
實(shí)現(xiàn)步驟:
1、安裝
npm:
npm install aplayer --saveYarn:
yarn add aplayer2、頁面中引入
import APlayer from 'APlayer'; import 'APlayer/dist/APlayer.min.css';3、具體使用,源代碼
(1)封裝 aPlayer.vue
<template><div class="mainPage" ref="playerRef"></div> </template><script setup>import APlayer from 'APlayer';import 'APlayer/dist/APlayer.min.css';import {reactive,nextTick, onBeforeUnmount,getCurrentInstance, onMounted, ref} from 'vue'const playerRef = ref()const { proxy } = getCurrentInstance()const state = reactive({instance:null})// APlayer歌曲信息class Audio {// 音頻藝術(shù)家// artist: String;// 音頻名稱// name: String;// 音頻鏈接// url: String;// 音頻封面// cover: String;// 歌詞// lrc: String;constructor(artist, name, url, cover, lrc) {this.artist = artist;this.name = name;this.url = url;this.cover = cover;this.lrc = lrc;}}const props = defineProps({// 開啟吸底模式fixed: {type: Boolean,default: false},// 開啟迷你模式mini: {type: Boolean,default: false},// 音頻自動(dòng)播放autoplay: {type: Boolean,default: false},// 主題色theme: {type: String,default: 'rgba(255,255,255,0.2)'},// 音頻循環(huán)播放loop: {type: String,default: 'all' //'all' | 'one' | 'none'},// 音頻循環(huán)順序order: {type: String,default: 'random' //'list' | 'random'},// 預(yù)加載preload: {type: String,default: 'auto' //'auto' | 'metadata' | 'none'},// 默認(rèn)音量volume: {type: Number,default: 0.7,validator: (value) => {return value >= 0 && value <= 1;}},// 歌曲服務(wù)器(netease-網(wǎng)易云, tencent-qq音樂, kugou-酷狗, xiami-小米音樂, baidu-百度音樂)songServer: {type: String,default: 'netease' //'netease' | 'tencent' | 'kugou' | 'xiami' | 'baidu'},// 播放類型(song-歌曲, playlist-播放列表, album-專輯, search-搜索, artist-藝術(shù)家)songType: {type: String,default: 'playlist'},// 歌的idsongId: {type: String,default: '19723756'},// 互斥,阻止多個(gè)播放器同時(shí)播放,當(dāng)前播放器播放時(shí)暫停其他播放器mutex: {type: Boolean,default: true},// 傳遞歌詞方式lrcType: {type: Number,default: 3},// 列表是否默認(rèn)折疊listFolded: {type: Boolean,default: true},// 列表最大高度listMaxHeight: {type: String,default: '100px'},// 存儲(chǔ)播放器設(shè)置的 localStorage keystorageName: {type: String,default: 'aplayer-setting'}})onMounted(() => {let str = {server:props.songServer,type:props.songType,id:props.songId}proxy.$api.common.getSongSheet(str).then(res=>{let audioList = res.data.map(value => new Audio(value.author, value.title, value.url, value.pic, value.lrc));state.instance = new APlayer({container: playerRef.value,fixed: props.fixed,mini: props.mini,autoplay: props.autoplay,theme: props.theme,loop: props.loop,order: props.order,preload: props.preload,volume: props.volume,mutex: props.mutex,lrcType: props.lrcType,listFolded: props.listFolded,listMaxHeight: props.listMaxHeight,storageName: props.storageName,audio: audioList})})// 銷毀onBeforeUnmount(() => {state.instance.destroy()})})</script><style lang='scss' scoped>.mainPage{@include wh(100%,auto);background: #FCFCFC;border: 1px solid #E0E0E0;border-radius: 4px;}</style>(2)父組件調(diào)用
<a-player></a-player>其他:
????????vue3+ts+aplayer版本:點(diǎn)我
總結(jié)
以上是生活随笔為你收集整理的vue3中实现音频播放器APlayer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS FTP客户端的见解
- 下一篇: InDesign 教程:如何将图形添加到