Reputation: 1668
I've searched and googled for quite a bit with nothing really able to resolve my issue.
I have an mp3 file on my server that I need to serve to my end users so they can hear the recording and verify that it's correct. The problem is that when listening, the browser (Safari and Chrome at least) will cut off the last 1-2 seconds of a recording. (which is not very helpful.
Stuff I've tried already.
file
and it's audio/mpeg; charset=binaryMessed around with the headers of the file with no luck.
header("Content-Type: audio/mpeg; charset=binary");
header("Content-Disposition: inline; filename=\"{$filename}\"");
header('Pragma: no-cache');
header("Content-Length: ". filesize($file_path));
readfile($file_path);
I've pretty much narrowed down the problem to something that happens from calling the file on my server to bringing it to my browser. The stuff I've tried hasn't really helped. One of my requirements is that I have to have this protected and out of a public directory, so I have to serve it through PHP as the user has to be authenticated before they can play the file. Any thoughts would be greatly appreciated.
Here's some versions if it helps.
CentOS 7.4.1708
Apache 2.4.6
PHP 5.6.30
Chrome Version 60.0.3112.113
Safari Version 10.1.2 (12603.3.8)
Update
The data from the main wav file I'm receiving is the following
Duration: 00:09
Audio Channels: 1
Sample Rate: 8,000
Bits Per Sample: 16
Upvotes: 0
Views: 1152
Reputation: 163293
A lot of audio players, particularly around MP3, are quite buggy. They'll use things like fixed-size buffers. Also, you should know that with MP3 it isn't possible to precisely set seek points without decoding the whole file. There are a lot of hacks/tricks around file length, byte offsets, and guessing, but that's about it. Basically, you can't ever expect timing to be correct with lossy encoded audio that has no container or bitstream format indicating what time it is. I do think it's reasonable to have your file played completely through though.
You should also know that your sample rate is extremely low, as is your bitrate, particularly for using stereo audio.
There's probably a fixed-size buffer somewhere that's expecting say 8KB of data and it's getting a small fraction of that due to the low sample rate and bitrate. This would be a player bug, and short of filing a bug report, there's nothing you can do about it directly.
Consider Opus? You'll get much higher quality, and it's quite compatible. Failing that, consider just using straight up PCM.
Upvotes: 1