zig_audio_kit 0.0.3 copy "zig_audio_kit: ^0.0.3" to clipboard
zig_audio_kit: ^0.0.3 copied to clipboard

A lightweight Flutter audio package providing audio recording and playback

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:zig_audio_kit/zig_audio_kit.dart';

class AudioKitScreen extends StatefulWidget {
  final String audioFilePath; // e.g., the path from your WAV recorder

  const AudioKitScreen({super.key, required this.audioFilePath});

  @override
  State<AudioKitScreen> createState() => _AudioKitScreenState();
}

class _AudioKitScreenState extends State<AudioKitScreen> {
  // Track states to update the UI buttons
  bool _isPlaying = false;
  bool _isPaused = false;

  void _play() {
    // Note: Assuming your class is named AudioPlayer as per the previous FFI setup.
    bool success = AudioPlayer.play(widget.audioFilePath);

    if (success) {
      setState(() {
        _isPlaying = true;
        _isPaused = false;
      });
    }
  }

  void _pause() {
    AudioPlayer.pause();
    setState(() {
      _isPlaying = false;
      _isPaused = true;
    });
  }

  void _resume() {
    AudioPlayer.resume();
    setState(() {
      _isPlaying = true;
      _isPaused = false;
    });
  }

  void _restart() {
    AudioPlayer.restart();
    setState(() {
      _isPlaying = true;
      _isPaused = false;
    });
  }

  void _stop() {
    AudioPlayer.stop();
    setState(() {
      _isPlaying = false;
      _isPaused = false;
    });
  }

  void startRecord() {
    // make sure permission for record
    AudioRecorder.start(widget.audioFilePath);
  }

  void stopRecord() {
    AudioRecorder.stop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Zig Audio Player")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Status Text
            Text(
              _isPlaying
                  ? "Playing..."
                  : (_isPaused ? "Paused" : "Ready to Play"),
              style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 40),

            // Buttons
            Wrap(
              spacing: 15,
              runSpacing: 15,
              alignment: WrapAlignment.center,
              children: [
                // Show PLAY if stopped or completely uninitialized
                if (!_isPlaying && !_isPaused)
                  ElevatedButton.icon(
                    onPressed: _play,
                    icon: const Icon(Icons.play_arrow),
                    label: const Text("Play"),
                  ),

                // Show PAUSE if currently playing
                if (_isPlaying)
                  ElevatedButton.icon(
                    onPressed: _pause,
                    icon: const Icon(Icons.pause),
                    label: const Text("Pause"),
                  ),

                // Show RESUME if currently paused
                if (_isPaused)
                  ElevatedButton.icon(
                    onPressed: _resume,
                    icon: const Icon(Icons.play_arrow),
                    label: const Text("Resume"),
                  ),

                // Show RESTART if there is an active session (playing or paused)
                if (_isPlaying || _isPaused)
                  ElevatedButton.icon(
                    onPressed: _restart,
                    icon: const Icon(Icons.replay),
                    label: const Text("Restart"),
                  ),

                // Show STOP if there is an active session (playing or paused)
                if (_isPlaying || _isPaused)
                  ElevatedButton.icon(
                    onPressed: _stop,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.red,
                      foregroundColor: Colors.white,
                    ),
                    icon: const Icon(Icons.stop),
                    label: const Text("Stop"),
                  ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
150
points
155
downloads

Documentation

API reference

Publisher

verified publishertokeyan.online

Weekly Downloads

A lightweight Flutter audio package providing audio recording and playback

Homepage

License

Apache-2.0 (license)

Dependencies

ffi, flutter, plugin_platform_interface

More

Packages that depend on zig_audio_kit

Packages that implement zig_audio_kit