Audio recording plugin for NativePHP Mobile with pause/resume support, background recording, and native permission handling.
The Microphone API provides access to the device's microphone for recording audio. It offers a fluent interface for starting and managing recordings, tracking them with unique identifiers, and responding to completion events.
composer require nativephp/mobile-microphoneuse Native\Mobile\Facades\Microphone;
// Start recording
Microphone::record()->start();
// Stop recording
Microphone::stop();
// Pause recording
Microphone::pause();
// Resume recording
Microphone::resume();
// Get status
$status = Microphone::getStatus();
// Returns: "idle", "recording", or "paused"
// Get last recording path
$path = Microphone::getRecording();import { microphone, on, off, Events } from '#nativephp';
// Basic recording
await microphone.record();
// With identifier for tracking
await microphone.record()
.id('voice-memo');
// Stop recording
await microphone.stop();
// Pause/resume
await microphone.pause();
await microphone.resume();
// Get status
const result = await microphone.getStatus();
if (result.status === 'recording') {
// Recording in progress
}
// Get last recording
const result = await microphone.getRecording();
if (result.path) {
// Process the recording
}Set a unique identifier for this recording.
Microphone::record()
->id('voice-note-123')
->start();Set a custom event class to dispatch when recording completes.
use App\Events\VoiceMessageRecorded;
Microphone::record()
->event(VoiceMessageRecorded::class)
->start();Store the recorder's ID in the session for later retrieval.
Microphone::record()
->id('voice-note')
->remember()
->start();Explicitly start the audio recording. Returns true if recording started successfully.
Dispatched when an audio recording completes.
Payload:
string $path- File path to the recorded audiostring $mimeType- MIME type of the audio (default:'audio/m4a')?string $id- The recorder's ID, if one was set
use Native\Mobile\Attributes\OnNative;
use Native\Mobile\Events\Microphone\MicrophoneRecorded;
#[OnNative(MicrophoneRecorded::class)]
public function handleAudioRecorded(string $path, string $mimeType, ?string $id)
{
$this->recordings[] = [
'path' => $path,
'mimeType' => $mimeType,
'id' => $id,
];
}import { on, off, Events } from '#nativephp';
import { ref, onMounted, onUnmounted } from 'vue';
const recordings = ref([]);
const handleAudioRecorded = (payload) => {
const { path, mimeType, id } = payload;
recordings.value.push({ path, mimeType, id });
};
onMounted(() => {
on(Events.Microphone.MicrophoneRecorded, handleAudioRecorded);
});
onUnmounted(() => {
off(Events.Microphone.MicrophoneRecorded, handleAudioRecorded);
});import { on, off, Events } from '#nativephp';
import { useState, useEffect } from 'react';
const [recordings, setRecordings] = useState([]);
const handleAudioRecorded = (payload) => {
const { path, mimeType, id } = payload;
setRecordings(prev => [...prev, { path, mimeType, id }]);
};
useEffect(() => {
on(Events.Microphone.MicrophoneRecorded, handleAudioRecorded);
return () => {
off(Events.Microphone.MicrophoneRecorded, handleAudioRecorded);
};
}, []);-
Microphone Permission: The first time your app requests microphone access, users will be prompted for permission. If denied, recording functions will fail silently.
-
Background Recording: You can allow your app to record audio while the device is locked by toggling
microphone_backgroundto true in the config. -
File Format: Recordings are stored as M4A/AAC audio files (
.m4a). This format is optimized for small file sizes while maintaining quality. -
Recording State: Only one recording can be active at a time. Calling
start()while a recording is in progress will returnfalse. -
Auto-Start Behavior: If you don't explicitly call
start(), the recording will automatically start when thePendingMicrophoneis destroyed.