PHP code example of pushinbr / pam-native-audio
1. Go to this page and download the library: Download pushinbr/pam-native-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/ */
pushinbr / pam-native-audio example snippets
use Pam\Native\Audio\{AudioPlayer, PlaybackProgress};
AudioPlayer::make('https://cdn.example.com/voice/8f1c.m4a')
->onProgress(function (PlaybackProgress $p): void {
$this->progress = $p->fraction(); // re-renders the component
})
->play();
use Pam\Native\Audio\{AudioPlayer, AudioRoute, PlaybackProgress, PlaybackRate, PlaybackState};
$player = AudioPlayer::make('voice/8f1c.m4a') // sandbox-relative path or https URL
->rate(PlaybackRate::X1_5)
->route(AudioRoute::Auto)
->progressInterval(250)
->onProgress(function (PlaybackProgress $p): void { $this->position = $p->positionMillis; })
->onStateChange(function (PlaybackState $s): void { $this->playing = $s === PlaybackState::Playing; })
->onEnd(function (): void { $this->position = 0; })
->onError(function (string $message): void { $this->error = $message; })
->play();
AudioPlayer::current()?->pause();
AudioPlayer::current()?->resume();
AudioPlayer::current()?->seek(12_000);
AudioPlayer::current()?->rate(AudioPlayer::current()->playbackRate()->nextVoiceNoteRate()); // 1× → 1.5× → 2×
AudioPlayer::stopAll();
AudioPlayer::make($notes[$i]->path)
->queue(...array_map(fn ($n) => $n->path, array_slice($notes, $i + 1)))
->onItemChange(fn (int $index, string $source) => $this->highlight($i + $index))
->onEnd(fn () => $this->highlight(null))
->play();
use Pam\Native\Audio\{AudioPlayer, AudioRoute, PlaybackProgress, PlaybackRate, PlaybackState};
final class VoiceNotePlayback
{
private static ?AudioPlayer $player = null;
private static PlaybackRate $rate = PlaybackRate::X1;
public static function start(string $source, array $following, int $startAtMs = 0): void
{
self::$player?->stop();
try {
self::$player = AudioPlayer::make($source)
->queue(...$following) // the next notes play without PHP
->rate(self::$rate)
->route(AudioRoute::Auto) // earpiece at the ear
->progressInterval(250)
->startAt($startAtMs) // tap on the waveform before playing
->onProgress(static fn (PlaybackProgress $p, AudioPlayer $player) => self::progressed($player, $p))
->onStateChange(static fn (PlaybackState $s, AudioPlayer $player) => self::stateChanged($player, $s))
->onItemChange(static fn (int $index, string $_source, AudioPlayer $player) => self::moveTo($player, $index))
->onEnd(static fn (AudioPlayer $player) => self::finished($player))
->onError(static function (string $message, AudioPlayer $player): void {
// A live rate change rejected while the note is preparing is not a playback failure.
if (!str_starts_with($message, 'setRate:')) {
self::failed($player);
}
});
self::$player->play();
} catch (Throwable) {
self::failed(self::$player);
}
}
public static function cycleRate(): void
{
self::$rate = self::$rate->nextVoiceNoteRate();
if (self::$player !== null && !self::$player->isStopped()) {
self::$player->rate(self::$rate); // applied live
}
}
}