WordPress.org

Ready to get started?Download WordPress

Forums

oEmbed HTML5 audio
How to add autoplay (3 posts)

  1. ros256
    Member
    Posted 1 year ago #

    Since this plugin doesn't support autoplay as written, I thought someone else might benefit from knowing how to modify the plugin to support autoplay.

    I simply modified line 35 of the plugin to add a reference to $attr{'autoplay'} in the <audio> tag as such:

    '<audio controls preload '.$attr{autoplay}.'><source src="%1$s" /><embed type="application/x-shockwave-flash" flashvars="audioUrl=%1$s" src="'.$flash_player.'" width="400" height="27" quality="best"></embed></audio>',

    Then reference the music file using [embed autoplay="autoplay"]URL_to_MP3[/embed]

    Pretty simple, hope it helps someone.

    http://wordpress.org/extend/plugins/oembed-html5-audio/

  2. AJ's WebCreations
    Member
    Posted 1 year ago #

    I got this code for auto play to work but only in IE and Chrome. In FF I have to start it my self and I need it to auto play any ideas?

  3. ros256
    Member
    Posted 1 year ago #

    Yeah, looks like that is true... I didn't modify the code that uses the flash plugin, but since you asked... I added a check for the autoplay parameter and referenced the result when generating the code for flash in Firefox and Opera. Try this :

    wp_embed_register_handler( 'html5_audio', '#^http://.+\.(mp3|ogg|wav)$#i', 'wp_embed_handler_html5_audio' );
    
    function wp_embed_handler_html5_audio( $matches, $attr, $url, $rawattr ) {
    	$flash_player = plugins_url('3523697345-audio-player.swf', __FILE__);
    
    	if ($attr{autoplay}=='autoplay') {
    		$autoplay='true';
    	} else {
    		$autoplay='false';
    	}
    
    	if (preg_match('#^http://.+\.mp3$#i', $url) && preg_match('/Firefox/', $_SERVER["HTTP_USER_AGENT"])) {
    		// For religious reasons Firefox does not support MP3 format in HTML5 audio tag, use Flash player instead
    		$embed = sprintf(
    				'<embed type="application/x-shockwave-flash" flashvars="audioUrl=%1$s&autoPlay='.$autoplay.'" src="'.$flash_player.'" width="400" height="27" quality="best"></embed>',
    				esc_attr($matches[0])
    				);
    	} else if (preg_match('#^http://.+\.mp3$#i', $url) && preg_match('/Opera/', $_SERVER["HTTP_USER_AGENT"])) {
    		// Opera also does not support MP3 format in HTML5 audio tag, use Flash player instead
    		$embed = sprintf(
    				'<embed type="application/x-shockwave-flash" flashvars="audioUrl=%1$s&autoPlay='.$autoplay.'" src="'.$flash_player.'" width="400" height="27" quality="best"></embed>',
    				esc_attr($matches[0])
    				);
    	} else if (preg_match('#^http://.+\.ogg$#i', $url) && preg_match('/MSIE/', $_SERVER["HTTP_USER_AGENT"])) {
    		$embed = '[Internet Explorer does not support OGG format]';
    	} else if (preg_match('#^http://.+\.wav$#i', $url) && preg_match('/MSIE/', $_SERVER["HTTP_USER_AGENT"])) {
    		$embed = '[Internet Explorer does not support WAV format]';
    	} else {
    		$embed = sprintf(
    				'<audio controls preload '.$attr{autoplay}.'><source src="%1$s" /><embed type="application/x-shockwave-flash" flashvars="audioUrl=%1$s" src="'.$flash_player.'" width="400" height="27" quality="best"></embed></audio>',
    				esc_attr($matches[0])
    				);
    	}
    
    	$embed = apply_filters( 'oembed_html5_audio', $embed, $matches, $attr, $url, $rawattr );
    	return apply_filters( 'oembed_result', $embed, $url, '' );
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic