B
B
Benelli2013-12-07 02:07:48
PHP
Benelli, 2013-12-07 02:07:48

How to open a popup window by clicking on a certain link, in which an mp3 file should start playing?

The task is relatively simple - by clicking on a certain link, open a popup window in which the mp3 file should start playing.
The first thing that came to mind was something like this:

header('Content-type: audio/mpeg');
header('Content-length: ' . filesize($filepath));
header('Content-Disposition: inline; filename="'.$filename.'"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($filepath);

But during testing, it turned out that while the script continues to send this mp3 file to the popup, this user cannot perform any other actions on the site, the rest of the requests are waiting for the current one to complete. That is, for example, if you click on another link, the browser will not go anywhere until the file is transferred.
The first question is why is this happening? It's not entirely obvious to me, it seemed to me that well, one active connection hangs - and let it hang, new ones will open for other requests from the same host.
Then I began to experiment with icecast2 and liquidsoap, they were installed on the server. I reasoned in such a way that, in principle, the task of streaming can be solved just with streaming tools. As a result, the php script creates a liquidsoap script on the fly and launches it for execution, which dynamically creates a broadcast mount point in icecast, to which we redirect the popup.
There is no problem with parallel requests in this solution, but there are two others - the file playback starts after a small delay due to stream buffering and, since the broadcast stream is played in the popup, the file is not downloaded and you cannot move the playback slider back and forth to different moments of the song .
So the second question - is it really better to stop at this second option? Or am I missing something and there are other options, how can I implement listening to a file through a php script?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rozhik, 2013-12-07
@rozhik

There are more tips in this solution:
1) the slider will only work in HTTP/1.1+. Accordingly, with PHP you will not be able to issue content (normally). Put nginx in front, and don't fool yourself. Or stream with streaming tools.
2) play audio via javascript || flash. (if already - describe how you managed to have similar problems)
There is not enough data for the first question to answer. In other places, everything works and no one canceled the firebug.
Refine the problem. (recently I helped a friend in a similar one, the related one is fresh in my memory)

A
Andrew, 2013-12-07
@kaasius

You are using a session. The first request blocks the session (this is a file on the server's disk, it is blocked, the next session will not start until this one is unlocked).
To stream media files, there are a lot of tools. For example - nginx_rtmp_module (if it's a live stream), or you can just stream through nginx. To control access, use X-Accel-Redirect.

A
Alexey Prokhorov, 2013-12-07
@megahertz

Try X-Sendfile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question