julimer's avatar

Audio file not working

Hi I'm currently experiencing this bug, I tried to show the audio file in the web page and I cant play the audio.

Here's my controller: public function createSong(Request $request) { // Initialize the $filename variable $filename = '';

    // Fetch the list of artists & albums from your database
    $artists = Artist::all();
    $albums = Album::all();

    // Validate input
    $validator = Validator::make($request->all(), [
        'title' => 'required|string',
        'artist_id' => 'required|exists:artists,id',
        'audio_file' => 'required|mimes:mp3,wav,ogg,flac',
        'duration' => 'nullable|numeric',
        'album_id' => 'required|exists:albums,id',
        'price' => 'required|numeric|min:0.00',
    ]);

    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator)->withInput();
    }

    // Handle audio file upload
    if ($request->hasFile('audio_file')) {
        $file = $request->file('audio_file');
        $filename = time() . '_' . $file->getClientOriginalName();

        // Store the file in the 'public/audio/songs/' directory
        $file->storeAs('public/audio/songs/', $filename);
        // $file->storeAs('audio', $filename, 'public');

        // Create a new song record in the database
        $song = Song::create([
            'title' => $request->input('title'),
            'artist_id' => $request->input('artist_id'),
            'audio_file' => 'public/audio/songs/' . $filename,
            'duration' => $request->input('duration'),
            'album_id' => $request->input('album_id'),
            'price' => $request->input('price'),
        ]);

        if ($song) {
            return redirect()->route('admin.dashboard')->with('success', 'Song uploaded successfully');
        } else {
            return redirect()->back()->with('error', 'Failed to save the song');
        }
    }

    return redirect()->back()->with('error', 'Failed to upload the song');
}

the file location on the directory is in storage/app/public/audio/songs. Both URL from the database and the debugging I made are just the same. No typo and all. Also I have already linked the storage using php artisan storage:link. I'm just wondering why the audio/song that I wanted to show on the web page is not playing. Do you guys have encountered this issue?

0 likes
2 replies
julimer's avatar

Here's for fetching the audio on my blade view:

        <audio controls>
            <source src="{{ asset($song->audio_file) }}" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>
 
julimer's avatar

got it fixed with this code:

        <audio controls>
            <source src="{{ Storage::url($song->audio_file) }}" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>
  
 

Please or to participate in this conversation.