PHP code example of nativephp / mobile-media-player
1. Go to this page and download the library: Download nativephp/mobile-media-player 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/ */
nativephp / mobile-media-player example snippets
use NativePHP\MediaPlayer\Facades\MediaPlayer;
// Play a bundled file, storage path, or URL
MediaPlayer::play(storage_path('app/theme.mp3'));
// With options
MediaPlayer::play('https://example.com/stream.mp3', [
'loop' => true,
'volume' => 0.5,
]);
// Transport controls
MediaPlayer::pause();
MediaPlayer::resume();
MediaPlayer::seek(42.5); // seconds
MediaPlayer::setVolume(0.8); // 0.0 – 1.0
MediaPlayer::stop(); // stops and releases the player
// Poll playback state
$status = MediaPlayer::getStatus();
// ['state' => 'playing', 'position' => 12.3, 'duration' => 180.0, 'source' => '...']
use NativePHP\MediaPlayer\Elements\VideoPlayer;
VideoPlayer::make($path)
->controls(false)
->autoplay()
->loop()
->muted();
use Native\Mobile\Attributes\OnNative;
use NativePHP\MediaPlayer\Events\PlaybackEnded;
#[OnNative(PlaybackEnded::class)]
public function handlePlaybackEnded(string $source)
{
$this->playNext();
}
use Native\Mobile\Attributes\OnNative;
use NativePHP\MediaPlayer\Events\PlaybackError;
#[OnNative(PlaybackError::class)]
public function handlePlaybackError(string $source, string $message)
{
$this->status = "Playback failed: {$message}";
}
use Native\Mobile\Testing\Native;
it('plays the selected track', function () {
Native::fakeBridge();
Native::test(NowPlaying::class)
->tap('Play')
->assertPlayed('https://example.com/stream.mp3');
});
it('shows where playback is', function () {
Native::fakeBridge()->withPlaybackStatus('playing', position: 12.0, duration: 180.0);
Native::test(NowPlaying::class)
->call('refresh')
->assertSee('0:12 / 3:00');
});
it('pauses, seeks, and stops', function () {
Native::fakeBridge();
Native::test(NowPlaying::class)
->tap('Pause')->assertPaused()
->tap('Skip forward')->assertSeeked(30.0)
->tap('Stop')->assertStopped();
});