Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #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!

    #1435308

    Hey 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
    Enfold_Support_4795.jpeg
    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:
    Enfold_Support_2680.jpeg
    then add the above code and save.
    You can adjust the css in the code above to suit your site.

    Best regards,
    Mike

    #1435751

    You are amazing, thank you thank you thank you!

    #1435782

    Hi,
    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

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Button Element – Can you make a button play an audio file upon click?’ is closed to new replies.