Saturday, 5 December 2020

How to Adjust the volume of media using JavaScript in PhoneGap?

 In our previous section, we made our own control buttons using JavaScript. In this section, we will learn how we can adjust the volume using JavaScript in PhoneGap. We will create a volume slider that will control the volume of our media. The volume is the property of our player, which can be easily controlled through the user interface.

These are the following steps used to create a volume slider in PhoneGap:

1) Create index2.html file

We will create another html file index2.html. This file will contain the same code as present in the index.html file. We will make changes in this file so that our index.html file remains unchanged.

Adjust the volume of media using JavaScript in PhoneGap

2) Create an input tag

Now, we will create slider in user interface using <input></input> tag and set type to range. We will also give an Id to it. We will set the maximum, and minimum value and set the value of step and value.

  1. <input type="range" id="rngVolume" min="0" max="1" step=".01" value=".5"/>  

3) Grab the volume slider

Now, we will grab the volume slider using JQuery notation. We will also use an event slide stop to call the anonymous function. We will get the slider using the document.getElementById. We will set the volume to the console using console.log() and the player volume in the following way:

  1. $('#rngVolume').on("slidestop", function(){  
  2.                     var volume = document.getElementById('rngVolume').value;  
  3.                     console.log(volume);  
  4.                     player.volume = volume;  
  5.                       
  6. });  


Adjust the volume of media using JavaScript in PhoneGap

Complete Project

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8">  
  5.         <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6.         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />  
  7.         <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>  
  8.         <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  
  9.         <title>Media</title>  
  10.         <script>  
  11.         var player;  
  12.               
  13.         window.onload=function()  
  14.         {  
  15.             player = document.getElementById('myMusic');  
  16.             document.getElementById('btnPlay').addEventListener('click', function(){  
  17.                player.play();   
  18.             });  
  19.             document.getElementById('btnPause').addEventListener('click' ,function(){  
  20.                 player.pause();     
  21.             });  
  22.             document.getElementById('btnStop').addEventListener('click', function(){  
  23.                 player.pause();  
  24.                 player.currentTime = 0;  
  25.             });  
  26.             $('#rngVolume').on("slidestop", function(){  
  27.                     var volume = document.getElementById('rngVolume').value;  
  28.                     console.log(volume);  
  29.                     player.volume = volume;  
  30.                       
  31.                 });  
  32.               
  33.         }  
  34.           
  35.         </script>  
  36.     </head>  
  37.     <body>  
  38.         <audio id="myMusic">  
  39.             <source src="media/camera.mp3"/>  
  40.         </audio>  
  41.         <button id="btnPlay">Play</button>  
  42.         <button id="btnPause">Pause</button>  
  43.         <button id="btnStop">Stop</button>  
  44.         <input type="range" id="rngVolume" min="0" max="1" step=".01" value=".5"/>  
  45.         <script type="text/javascript" src="cordova.js" ></script>  
  46.     </body>  
  47. </html>  


No comments:

Post a Comment