PHP code example of theunwindfront / nativephp-audio

1. Go to this page and download the library: Download theunwindfront/nativephp-audio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

theunwindfront / nativephp-audio example snippets


use Theunwindfront\Audio\Facades\Audio;

// 1. Play a single track with metadata
Audio::play('https://example.com/song.mp3', [
    'title'    => 'Midnight City',
    'artist'   => 'M83',
    'artwork'  => 'https://example.com/artwork.jpg',
]);

// 2. Play a Local File (Mobile Storage)
// Raw paths from storage_path() are natively supported
Audio::play(storage_path('app/public/recordings/audio.mp3'), [
    'title'  => 'Voice Note',
    'artist' => 'Recorded Local'
]);

// 3. Manage a Playlist (Natively handled auto-advance)
Audio::setPlaylist([
    [
        'url'   => 'https://example.com/track1.mp3',
        'title' => 'Track 01',
    ],
    // ... more tracks
], autoPlay: true, startIndex: 0);

// 4. Playback Controls
Audio::pause();
Audio::resume();
Audio::next();
Audio::previous();
Audio::skipTo(5); // Skip to index 5 in playlist

// 5. State & Settings
$state = Audio::getState(); 
Audio::setVolume(0.8);
Audio::setPlaybackRate(1.5);
Audio::setShuffleMode(true);
Audio::setRepeatMode('all'); // 'none', 'one', 'all'

// 6. Sleep Timer
Audio::setSleepTimer(1800); // 30 minutes

use Theunwindfront\Audio\Facades\Audio;

// Play a stream by direct URL
Audio::playStream('https://stream.example.com/live.mp3', [
    'title'   => 'My Radio Station',
    'artist'  => 'Live DJ Set',
    'artwork' => 'https://example.com/radio-logo.jpg',
]);

// Play a stream by mountpoint (resolved against a server URL)
Audio::playStream('/live', [
    'serverUrl' => 'https://stream.example.com',
    'title'     => 'Main Channel',
    'artist'    => 'Now Playing',
]);

// Update metadata in real-time (e.g., when the current song changes)
Audio::updateStreamMetadata([
    'title'   => 'New Song Title',
    'artist'  => 'New Artist',
    'artwork' => 'https://example.com/new-artwork.jpg',
]);

$missedEvents = Audio::drainEvents();

Audio::play('/storage/emulated/0/Download/my-song.mp3');