Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

carios1's avatar

Streaming Audio

I'm building an application that is focused on streaming audio to my users. I've never actually built something of this liking so I was wondering if I could get some advice on how to serve the actual files. I'm using MediaElement.js to play the files, but how would you go about serving them to the frontend?

P.s. sorry If I'm not supposed to post problems on here. I wasn't sure where to put this.

0 likes
5 replies
bashy's avatar

Depends how secure and non-hotlinking you want it to be. Obviously if you supply an audio file, people will be able to see the stream URL and just download it. Take a look at how SoundCloud and other audio services do theirs.

It's fine to post it here, main reason for the "forums".

1 like
carios1's avatar

Yeah thats always the problem with supplying the audio. I'm going to be a little lax with it because users will have the ability to download the audio as well. I'm not even sure where to start looking with SoundCloud.

ovidiu_dtp's avatar

Hello, I would love to see something about multimedia management and best practices for upload and display, eventually how you deal with file conversions, if it is worth the effort of converting user files, considering is a processor intensive task... I need to create a video gallery with user generated content. I am still researching how I should go about it... For now, I decided against converting the uploaded files, but this may hurt the usability of the website in ways I do not understand yet...

1 like
equipc's avatar

@carios1 : I'm working on a similar project as yours and I have an issue with serving mp3 file from Laravel. Did you find a solution ?

My problem is that duration is shown as NaN:NaN in the MediaElement audio player. The problem is when serving file from Laravel. If I directly use a file in public folder, it is working great.

My code :

public function listen($id)
{
        $filename = base_path('resources/audio/' . $id . '.mp3');
        $filesize = (int) File::size($filename);

        $file = File::get($filename);

        $response = Response::make($file, 200);
        $response->header('Content-Type', 'audio/mpeg');
        $response->header('Content-Length', $filesize);
        $response->header('Accept-Ranges', 'bytes');
        $response->header('Content-Range', 'bytes 0-'.$filesize.'/'.$filesize);

        return $response;
}
2 likes

Please or to participate in this conversation.