1. <template>
    2. <div>
    3. <p @click="getDom" ref="music">音乐播放</p>
    4. <img :src="isPlay?require('@/assets/images/play.png'):require('@/assets/images/pause.png')"
    5. @click="handlePlay"/>
    6. <audio :src="playUrl" controls ref="audio"
    7. @play="onPlay" @pause="onPause"></audio>
    8. </div>
    9. </template>
    10. <script>
    11. export default {
    12. name:"Audio",
    13. data(){
    14. return{
    15. isPlay:false,
    16. playUrl:"http://m10.music.126.net/20191113111139/3518f37fdcbee4b02601a04edda5da83/ymusic/e54a/90bc/8f90/29b0d3a279414b2f9a1fd78c64a907cc.mp3"
    17. }
    18. },
    19. methods:{
    20. handlePlay(){
    21. if(this.isPlay){
    22. this.$refs.audio.pause()
    23. this.isPlay=false
    24. }else{
    25. this.$refs.audio.play()
    26. this.isPlay=true
    27. }
    28. },
    29. getDom(){
    30. console.log(this.$refs.music)
    31. },
    32. /* 监听音乐播放状态 */
    33. onPlay(){
    34. this.isPlay=false
    35. },
    36. /* 监听音乐暂停状态 */
    37. onPause(){
    38. this.isPlay=true
    39. }
    40. }
    41. }
    42. </script>
    43. <style scoped>
    44. </style>

    如果不想要audio的显示条

    1. <style scoped>
    2. audio{
    3. visibility:hidden;
    4. }
    5. </style>