Saturday, 5 December 2020

How to do Media Position and Duration in PhoneGap?

 

Media Position and Duration

Previously, we learned about how we can play audio and video content, control playback of media, and adjust the volume of media. In this section, we will learn about media position and duration. When we work with media, we also often want to know how long a specific media piece is and where we are in that specific piece of media.

These are the following steps used to get the media position and duration.

1) Create index3.html

We will create a new html file namely index3.html with the same content present in the index.html. We will make changes in the index3.html file without affecting the code present in the index.html.

Media Position and Duration

2) Create an output field in the user interface

Now, we will create output fields for position and duration using <output></output> tags. These output tags are used to show media position and duration in the following way:

  1. <output id="outPosition"></output>/<output id="outDuration"></output>  

3) outDuration

Now, we will get the outDuration field using Id and assign the duration of the player by converting minutes into seconds in the following way:

  1. document.getElementById('outDuration').innerHTML = Math.round(player.duration) + " seconds";  


Media Position and Duration

4) outPosition

Now, we will find the interval by using the setInterval() function. This function contains two arguments, i.e., display time and duration. We will create displayTime function outside the window.onload function in the following way:

  1. intv = setInterval(displayTime, 500);<!?This line will be added in onload function-->  
  2. function displayTime(e)  
  3. {  
  4.             document.getElementById('outPosition').innerHTML = Math.round(player.currentTime);  
  5. }  


Media Position and Duration

Complete Code:

  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.         <title>Audio and Video Example</title>  
  7.         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />  
  8.         <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>  
  9.         <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  
  10.         <script>  
  11.         var player;  
  12.           
  13.         window.onload = function()  
  14.         {  
  15.             player = document.getElementById('mysong');  
  16.             document.getElementById('Play').addEventListener('click', function(){  
  17.                 player.play();  
  18.                 document.getElementById('outDuration').innerHTML = Math.round(player.duration) + " seconds";  
  19.                 intv = setInterval(displayTime, 500);  
  20.             });  
  21.             document.getElementById('Pause').addEventListener('click', function(){  
  22.                 player.pause();  
  23.             });  
  24.             document.getElementById('Stop').addEventListener('click', function(){  
  25.                 player.pause();  
  26.                 player.currentTime = 0;  
  27.             });  
  28.               
  29.             $('#rngVolume').on("slidestop", function(){  
  30.             var volume = document.getElementById('rngVolume').value;  
  31.             console.log(volume);  
  32.             player.volume = volume;  
  33.                       
  34.             });  
  35.         }  
  36.         function displayTime(e)  
  37.         {  
  38.             document.getElementById('outPosition').innerHTML = Math.round(player.currentTime);  
  39.         }  
  40.         </script>  
  41.     </head>  
  42.     <body>  
  43.         <audio id="mysong">  
  44.         <source src="media/Saki.mp3"/>  
  45.         </audio>  
  46.         <button id="Play">Play</button>  
  47.         <button id="Pause">Pause</button>  
  48.         <button id="Stop">Stop</button>  
  49.         <output id="outPosition"></output>  
  50.         <output id="outDuration"></output>  
  51.         <script type="text/javascript" src="cordova.js"></script>  
  52.     </body>  
  53. </html>  


No comments:

Post a Comment