One play pause button for audio

Currently i have a single button that when you click on it it will play the file in the data-url.  But the problem is how do i stop the playback of the audio when i click the button again.  I have included my code below.   So basically a pause play button.
<button data-url="as1"></button>

Open in new window



    $( document ).ready(function() {
           $('.btnAudio').click(function(){
                var audioplayer = document.getElementById("brandAudio");
                var audioFile=$(this).attr('data-url');
                var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
                var audioExt='.mp3';
                var audioFileName="";
                var plyClass=$(this).attr('class').replace('icon-star-half-empty process-icon btnAudio','');

                 if(isFirefox==true){
                     audioExt=".ogv";
                 }


//               audioplayer.addEventListener('playing',function()
//               {
//                   var audioSrc=audioplayer.src.replace('http://www.devassist.com','');
//                   if(audioSrc===audioFileName){
//                       audioplayer.pause();
//                       alert(audioSrc);
//                   }
//               },false);


                audioFileName="/audio/"+audioFile+audioExt;
                audioplayer.src=audioFileName;
                audioplayer.load();
                audioplayer.pause();
                audioplayer.play();

            })



        });

Open in new window

    $( document ).ready(function() {
           $('.btnAudio').click(function(){
                var audioplayer = document.getElementById("brandAudio");
                var audioFile=$(this).attr('data-url');
                var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
                var audioExt='.mp3';
                var audioFileName="";
                var plyClass=$(this).attr('class').replace('icon-star-half-empty process-icon btnAudio','');

                 if(isFirefox==true){
                     audioExt=".ogv";
                 }


//               audioplayer.addEventListener('playing',function()
//               {
//                   var audioSrc=audioplayer.src.replace('http://www.devassist.com','');
//                   if(audioSrc===audioFileName){
//                       audioplayer.pause();
//                       alert(audioSrc);
//                   }
//               },false);


                audioFileName="/audio/"+audioFile+audioExt;
                audioplayer.src=audioFileName;
                audioplayer.load();
                audioplayer.pause();
                audioplayer.play();

            })



        });

Open in new window

stargateatlantisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
You will need to attach this to the play/pause button ID
 
var audioplayer = document.getElementById("brandAudio");
if (audioplayer.paused!=false) {
      audioplayer.play();
  } else {
      audioplayer.pause();
  }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stargateatlantisAuthor Commented:
I tired that but it doesn't work at all

 $( document ).ready(function() {
           $('.btnAudio').click(function(){
                var audioplayer = document.getElementById("brandAudio");
                var audioFile=$(this).attr('data-url');
                var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
                var audioExt='.mp3';
                var audioFileName="";
                var plyClass=$(this).attr('class').replace('icon-star-half-empty process-icon btnAudio','');

                 if(isFirefox==true){
                     audioExt=".ogv";
                 }


               audioFileName="/audio/"+audioFile+audioExt;
               audioplayer.src=audioFileName;
               audioplayer.load();
           
               if (audioplayer.paused!=false) {
                   audioplayer.play();
               } else {
                   audioplayer.pause();
               }




            })



        });

Open in new window

0
GaryCommented:
Got a link for me to see it?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.