-
AuthorPosts
-
February 23, 2024 at 3:59 pm #1435223
Hi,
I was just wondering if it was possible to have a button play an audio file upon click? I’ve added the .mp3 file as the link for the button to open, but that brings the audio file up in a new tab or window and I would love to use a button to simply click and play the audio file.
Thanks for your input!
February 24, 2024 at 6:01 pm #1435308Hey Eleina,
I didn’t find a way to do this so I created a shortcode to create a button that will play a audio file and show a progress bar for the audio track while it is playing
The shortcode to use on your page:
[audio_player src="URL_TO_YOUR_AUDIO_FILE.mp3" text="Custom Button Text"]
Then add this code to the end of your child theme functions.php file in Appearance ▸ Editor:// use this shortcode: [audio_player src="URL_TO_YOUR_AUDIO_FILE.mp3" text="Custom Button Text"] function custom_audio_player_shortcode($atts) { $atts = shortcode_atts( array( 'src' => '', 'text' => 'Play Audio', ), $atts, 'audio_player' ); // Generate a unique ID for this instance $uid = uniqid('audio_player_'); ob_start(); ?> <style> .custom-audio-player { position: relative; max-width:200px; margin-bottom:10px; } .audio-control { position: relative; overflow: hidden; padding: 10px; border: none; background-color: #007bff; color: #ffffff; cursor: pointer; width:200px; transition: background-color 0.3s ease; } .audio-progress { position: absolute; bottom: 0; left: 0; height: 5px; background-color: #ff0000; width: 0; max-width: 100%; transition: width 0.1s linear; } </style> <div class="custom-audio-player" id="<?php echo $uid; ?>"> <button class="audio-control"><?php echo esc_html($atts['text']); ?></button> <div class="audio-progress"></div> </div> <audio class="custom-audio" src="<?php echo esc_url($atts['src']); ?>"></audio> <script> document.addEventListener('DOMContentLoaded', function() { var player = document.getElementById('<?php echo $uid; ?>'); var audio = player.nextElementSibling; var button = player.querySelector('.audio-control'); var progress = player.querySelector('.audio-progress'); var originalButtonText = "<?php echo esc_js($atts['text']); ?>"; button.addEventListener('click', function() { if (audio.paused) { audio.play(); button.textContent = 'Pause Audio'; } else { audio.pause(); button.textContent = originalButtonText; // Use originalButtonText instead of hardcoding } }); audio.addEventListener('timeupdate', function() { var duration = audio.duration; var currentTime = audio.currentTime; var progressWidth = (currentTime / duration) * 100 + '%'; progress.style.width = progressWidth; }); audio.addEventListener('ended', function() { button.textContent = originalButtonText; }); }); </script> <?php return ob_get_clean(); } add_shortcode('audio_player', 'custom_audio_player_shortcode');
Please ensure to copy the code from the forum and not an email notification so the symbols are not converted.
If you are not using a child theme you could use the WP Code plugin then add a new snippet, in the top right corner use the PHP snippet as the code type:
then add the above code and save.
You can adjust the css in the code above to suit your site.Best regards,
MikeFebruary 28, 2024 at 2:40 pm #1435751You are amazing, thank you thank you thank you!
February 28, 2024 at 6:20 pm #1435782Hi,
Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.Best regards,
Mike -
AuthorPosts
- The topic ‘Button Element – Can you make a button play an audio file upon click?’ is closed to new replies.