简体   繁体   English

MP3 文件不在自定义控件中播放

[英]MP3 file not playing within a custom control

So there's this really cool button I want to use:所以我想使用这个非常酷的按钮:

https://css-tricks.com/making-pure-css-playpause-button/ https://css-tricks.com/making-pure-css-playpause-button/

The 2 problems I'm having is I can't play the sound.我遇到的两个问题是我无法播放声音。 I've put the mp3 file in the root directory so I'm assuming I need to put something like我已将 mp3 文件放在根目录中,所以我假设我需要放置类似

<button class='button'>
  <sound src="play.mp3" type="audio/mpeg">
</button>

When I use the standard audio control it works fine but I really want to use this cool beans button.当我使用标准音频控制时,它工作得很好,但我真的很想使用这个很酷的 bean 按钮。 Also if I want to put text to the right of the button how would I do?另外,如果我想将文本放在按钮的右侧,我该怎么办?

Also if I want to have a list of audio files how would that be achieved?另外,如果我想要一个音频文件列表,那将如何实现?

Thanks for your help谢谢你的帮助

You can use html like this您可以像这样使用 html

<button onclick="play_pause()">play/pause</button>
<audio src="music.mp3" id="music"></audio>

And javascript like this和 javascript 这样

function play_pause(){

            if(document.getElementById("music").paused){ //check whether music is paused
            document.getElementById("music").play();     //if paused then play it
            }

            else{document.getElementById("music").pause();} //else pause it
        }

To put text to the right of the button you would put everything in another with a display: flex;要将文本放在按钮的右侧,您可以将所有内容放在另一个带有显示的地方: flex; attribute and flex-direction: row;属性和弹性方向:行;

For the audio, I create a new audio object in javascript.对于音频,我在 javascript 中创建了一个新的音频 object。 Then I can easily start and stop it from the code.然后我可以轻松地从代码中启动和停止它。

To use multiple sounds, we edit the toggleSound() function to use a string for the song name.要使用多种声音,我们编辑 toggleSound() function 以使用字符串作为歌曲名称。 Then we loop over the string to check if we have a sound associated with it.然后我们遍历字符串以检查是否有与之关联的声音。 This is infinitely expandable.这是无限扩展的。 Just add more audio objects, and new cases in the switch statement and buttons to control them.只需在 switch 语句和按钮中添加更多音频对象和新案例来控制它们。

You can see that in each button we have to provide the audio name, so you can have multiple buttons per sound.您可以看到,在每个按钮中,我们必须提供音频名称,因此每个声音可以有多个按钮。

 // some example audio const song1 = new Audio( "https://ia800905.us.archive.org/19/items/FREE_background_music_dhalius/backsound.mp3" ); // some other audio const song2 = new Audio( "https://ia800905.us.archive.org/19/items/FREE_background_music_dhalius/FormulaFantasy.mp3" ); function toggleSound(audioName) { // switch for every case of the song name. switch(audioName.toString()) { case "song1": var x = song1; break; case "song2": var x = song2; break; default: // set default song var x = song1; } if (x.paused) { x.play(); } else { x.pause(); } }
 .button { margin: 0 10px 10px 0; background: lightgray; }.button:hover { cursor: pointer; }.parent { display: flex; flex-direction: column; }
 <div class="parent"> <div style="display: flex; flex-direction: row;"> <;-- your cool button div --> <div onclick="toggleSound('song1')." class="button"> Song 1 </div> Neat button for the first song: </div> <div style="display; flex: flex-direction; row;"> <.-- your cool button div --> <div onclick="toggleSound('song2');" class="button"> Song 2 </div> Button for the second song. </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM