• 如何写最基础的播放音频界面


    这篇就是基础介绍写法

    这里要用到的标签是它其实和

    < img src=" "> 

    < audio src=" ">

    < video src=" ">

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>听音小屋</title>
    6. </head>
    7. <body>
    8. <audio src="./光年之外.mp3" ></audio>
    9. </body>
    10. </html>

    但是光是如此播放不了的,没有控制面板

    所以我们要加上它的controls属性

    这样才能播放

    关于它的js呢

    在控制台上操作

     我们可以看出oAudio的类型是一个对象,而这个对象实际上就是HTMLAudioElement

    所以我们可以通过这个对象控制播放器

    直接上方法

    oAudio.src="……"  换歌

    oAudio.play()          播放

    oAudio.pause()       暂停

    oAudio.currentTime当前时间(修改oAudio.currentTime也可以修改当前播放位置)

    oAudio.duration      总时长

    oAudio.muted          静音(如果oAudio.muted =true就静音,false就取消静音)

    oAudio.volume        控制声音大小(最大值为1)

    利用这些方法至少可以做出能用的播放器

    具体写法

    1. <!DOCTYPE html>
    2. <html lang="zh-hans">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>播放音乐</title>
    6. <style>
    7. body {
    8. display: flex;
    9. flex-direction: column;
    10. }
    11. </style>
    12. </head>
    13. <body>
    14. <audio src="./光年之外.mp3"></audio>
    15. <button class="playPause">播放</button>
    16. <button class="prev">上一首</button>
    17. <button class="next">下一首</button>
    18. <div>当前时间: <span class="currentTime"></span></div>
    19. <div>总时间: <span class="duration"></span></div>
    20. <div>
    21. <input type="text" id="progress"><button class="jumpTo">跳到此处</button>
    22. </div>
    23. <button class="volumeUp">声音增加</button>
    24. <button class="volumeDown">声音减少</button>
    25. <script>
    26. var mp3 = ['光年之外', '句号', '诀爱']
    27. var currentIndex = 0
    28. var playing = false
    29. var oAudio = document.querySelector('audio')
    30. document.querySelector('.playPause').onclick = function() {
    31. if (playing) {
    32. oAudio.pause()
    33. this.textContent = '播放'
    34. } else {
    35. oAudio.play()
    36. this.textContent = '暂停'
    37. }
    38. playing = !playing
    39. }
    40. document.querySelector('.prev').onclick = function() {
    41. currentIndex--;
    42. if (currentIndex < 0) {
    43. currentIndex = 2;
    44. }
    45. var name = "./" + mp3[currentIndex] + ".mp3";
    46. oAudio.src = name
    47. }
    48. document.querySelector('.next').onclick = function() {
    49. currentIndex++;
    50. if (currentIndex > 2) {
    51. currentIndex = 0;
    52. }
    53. var name = "./" + mp3[currentIndex] + ".mp3";
    54. oAudio.src = name
    55. }
    56. oAudio.ontimeupdate = function() {
    57. var ct = oAudio.currentTime
    58. var ctm = Math.floor(ct / 60)
    59. var cts = Math.floor(ct % 60)
    60. document.querySelector('.currentTime').textContent = ctm + ":" + cts
    61. var dt = oAudio.duration
    62. var dtm = Math.floor(dt / 60)
    63. var dts = Math.floor(dt % 60)
    64. document.querySelector('.duration').textContent = dtm + ":" + dts
    65. }
    66. document.querySelector('.jumpTo').onclick = function() {
    67. var p = parseInt(document.querySelector('#progress').value)
    68. if (p < 0) { p = 0 }
    69. if (p > 100) { p = 100 }
    70. var ct = p / 100 * oAudio.duration;
    71. oAudio.currentTime = ct
    72. document.querySelector('#progress').value = ''
    73. }
    74. document.querySelector('.volumeUp').onclick = function() {
    75. var volume = oAudio.volume;
    76. volume += 0.1
    77. if (volume > 1) {
    78. volume = 1
    79. }
    80. oAudio.volume = volume
    81. }
    82. document.querySelector('.volumeDown').onclick = function() {
    83. var volume = oAudio.volume;
    84. volume -= 0.1
    85. if (volume < 0) {
    86. volume = 0
    87. }
    88. oAudio.volume = volume
    89. }
    90. </script>
    91. </body>
    92. </html>

     

  • 相关阅读:
    cas与volatile
    FFTW库在Windows中的安装和使用
    设计一个网络爬虫(Python)
    玩推特营销必知的基础常识上篇
    Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
    JAVA常用工具类
    基于springboot地方废物回收机构管理系统springboot11
    SPFA和链式前向星
    dhtmlx-gantt甘特图数据展示
    【找不到视图问题解决】@RestController 与 @Controller注解的使用区别
  • 原文地址:https://blog.csdn.net/MM2LYF/article/details/126714712