后台代码(只支持本地文件)
//获取音频文件时长star
include_once 'get_mp3_time.inc';
$filename = $showphpath;
// $filename = 'test.mp3';
$mp3 = new MP3File($filename);
$a = $mp3->getDurationEstimate();
$b = $mp3->getDuration();
$duration = $mp3::formatTime($b);
$audio_time = 0;//时长(s)
if(count($duration)>0){
$audio_time = ceil($duration['hours']*3600+$duration['minutes']*60+$duration['seconds']);
}
//获取音频文件时长end
获取长度类
<?php
class MP3File
{
protected $filename;
public function __construct($filename)
{
$this->filename = $filename;
}
public static function formatTime($duration) //as hh:mm:ss
{
//return sprintf("%d:%02d", $duration/60, $duration%60);
$hours = floor($duration / 3600);
$minutes = floor( ($duration - ($hours * 3600)) / 60);
$seconds = $duration - ($hours * 3600) - ($minutes * 60);
// return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
return array('hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$seconds);
}
//Read first mp3 frame only... use for CBR constant bit rate MP3s
public function getDurationEstimate()
{
return $this->getDuration($use_cbr_estimate=true);
}
//Read entire file, frame by frame... ie: Variable Bit Rate (VBR)
public function getDuration($use_cbr_estimate=false)
{
$fd = fopen($this->filename, "rb");
$du