get audio file duration in jquery

Solutions on MaxInterview for get audio file duration in jquery by the best coders in the world

showing results for - "get audio file duration in jquery"
Bloom
09 Apr 2019
1function analyize_mp3_add_meta_box() {
2	add_meta_box(
3		'analyize_mp3-analyize-mp3',
4		__( 'Analyize MP3', 'analyize_mp3' ),
5		'analyize_mp3_html',
6		'post',
7		'normal',
8		'default'
9	);
10}
11add_action( 'add_meta_boxes', 'analyize_mp3_add_meta_box' );
12function analyize_mp3_html( $post) {
13wp_nonce_field( '_analyize_mp3_nonce', 'analyize_mp3_nonce' ); ?>
14<style type="text/css">
15    audio {display: none;}
16</style>
17<script type='text/javascript'>//<![CDATA[
18    jQuery(function(){
19    var objectUrl;
20    jQuery("#audio").on("canplaythrough", function(e){
21        var seconds = e.currentTarget.duration;
22        var duration = moment.duration(seconds, "seconds");
23        var time = "";
24        var hours = duration.hours();
25        if (hours > 0) { time = hours + ":" ; }
26        time = time + duration.minutes() + ":" + duration.seconds();
27        jQuery("#duration").text(time);
28        jQuery('#timelength').val(time);
29        URL.revokeObjectURL(objectUrl);
30    });
31    jQuery("#file").change(function(e){
32        var file = e.currentTarget.files[0];
33        jQuery("#filesize").text(file.size);
34        jQuery('#size').val(file.size);
35        objectUrl = URL.createObjectURL(file);
36        jQuery("#audio").prop("src", objectUrl);
37    });
38    });//]]> 
39</script>    
40<input type="file" id="file" />    
41<audio id="audio"></audio>    
42<p>
43    <label>File Size:</label>
44    <span id="filesize"></span>
45    <input type="hidden" id="size" name="size" value=""/>
46</p>    
47<p>
48    <label>Song Duration:</label>
49    <span id="duration"></span>
50    <input type="hidden" id="timelength" name="time" value=""/>
51</p>        
52<?php }