How to play a hidden AMP-Audio on AMP-Image click on an AMP-HTML page (Full Page Example)?

158 Views Asked by At

How to play a hidden audio file, presumably via an AMP-Audio (<amp-audio></amp-audio>) component on a AMP-HTML page element click, specifically an AMP Image (<amp-img></amp-img>) click?

Specifically, what I'm ultimately trying to do is duplicate the functionality of the speaker image beside my name on this page: https://m.jamesandersonjr.com, on this page: https://amp.jamesandersonjr.com, but the page must validate following all known AMP rules.

Please let me know if this question is a duplicate, but I could not find a similar suitable solution anywhere on S.O.

1

There are 1 best solutions below

0
James Anderson Jr. On

I solved the issue. Here is an example solution for those who need it.

<!DOCTYPE html>
<html amp lang="en">

<head>

<meta charset="utf-8">

<title>AMP Validated Demo: Play Audio File on AMP Image Click</title>

<link rel="canonical" href="https://www.example.com">

<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">

<style amp-boilerplate>

body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}

</style>

<noscript>

<style amp-boilerplate>

body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}

</style>

</noscript>

<style amp-custom>

h1{font-family:Arial, Helvetica, sans-serif;color:#006EDB;font-size:188%;font-weight:600;}

</style>

<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-audio" src="https://cdn.ampproject.org/v0/amp-audio-0.1.js"></script>

</head>

<body>

<h1>How to Play a Hidden Audio File on Click</h1>

<amp-img width="32" height="32" alt="&#128266; Hear Audio Track." title="&#128266; Hear Audio Track." role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