audio_recorder_service_flutter 1.0.0+1 copy "audio_recorder_service_flutter: ^1.0.0+1" to clipboard
audio_recorder_service_flutter: ^1.0.0+1 copied to clipboard

SDK Updated to 3.8.0

example/lib/main.dart

import 'package:audio_recorder_service/audio_recorder_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:permission_handler/permission_handler.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize audio service for lock screen
  await JustAudioBackground.init(
    androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio',
    androidNotificationChannelName: 'Audio playback',
    androidNotificationOngoing: true,
    androidShowNotificationBadge: true,
    preloadArtwork: true,
  );

  await initializeService();
  await initializeNotifications();
  runApp(MyApp());
}
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();



Future<void> initializeNotifications() async {
  if (await Permission.notification.isDenied) {
    await Permission.notification.request();
  }

  const AndroidInitializationSettings initializationSettingsAndroid =
  AndroidInitializationSettings('@mipmap/ic_launcher');

  const InitializationSettings initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
  );

  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
    onDidReceiveNotificationResponse: (NotificationResponse response) async {
      if (response.payload != null) {
        debugPrint("Notification clicked with payload: ${response.payload}");
      }
    },
  );
}



Future<void> initializeService() async {
  final service = FlutterBackgroundService();
  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: false,
      isForegroundMode: true,
      notificationChannelId: 'audio_record_channel',
      initialNotificationTitle: 'Audio Recording Service',
      initialNotificationContent: 'Recording in progress',
      foregroundServiceNotificationId: 888,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: false,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
  return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
  service.on('stopService').listen((event) {
    service.stopSelf();
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Audio Recorder',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        brightness: Brightness.light,
      ),
      darkTheme: ThemeData(
        primarySwatch: Colors.blue,
        brightness: Brightness.dark,
      ),
      home: AudioRecorderPage(),
    );
  }
}

class AudioRecorderPage extends StatefulWidget {
  @override
  _AudioRecorderPageState createState() => _AudioRecorderPageState();
}

class _AudioRecorderPageState extends State<AudioRecorderPage> {
  AudioRecorderService audioRecorderService = AudioRecorderService();

  @override
  void initState() {
    super.initState();
    audioRecorderService.initializeNotifications();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Audio Recorder')),
      body: Center(
        child: ValueListenableBuilder<RecordingState>(
          valueListenable: audioRecorderService.recordingStateNotifier,
          builder: (context, recordingState, child) {
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  recordingState == RecordingState.recording
                      ? Icons.mic
                      : (recordingState == RecordingState.paused
                      ? Icons.pause_circle
                      : Icons.mic_none),
                  size: 100,
                  color: recordingState == RecordingState.recording
                      ? Colors.red
                      : (recordingState == RecordingState.paused
                      ? Colors.orange
                      : Colors.blue),
                ),
                const SizedBox(height: 40),

                // Recording Controls
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ElevatedButton.icon(
                      icon: Icon(
                        recordingState == RecordingState.recording
                            ? Icons.pause
                            : (recordingState == RecordingState.paused
                            ? Icons.play_arrow
                            : Icons.fiber_manual_record),
                      ),
                      label: Text(
                        recordingState == RecordingState.recording
                            ? 'Pause'
                            : (recordingState == RecordingState.paused
                            ? 'Resume'
                            : 'Record'),
                      ),
                      onPressed: () async {
                        if (recordingState == RecordingState.recording) {
                          await audioRecorderService.pauseRecording();
                        } else if (recordingState == RecordingState.paused) {
                          await audioRecorderService.resumeRecording();
                        } else {
                          await audioRecorderService.startRecording();
                        }
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: recordingState == RecordingState.recording
                            ? Colors.orange
                            : (recordingState == RecordingState.paused
                            ? Colors.green
                            : Colors.blue),
                        padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 40),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ElevatedButton.icon(
                      icon: const Icon(Icons.check, color: Colors.white),
                      label: const Text('Save & Stop'),
                      onPressed: () async {
                        if (recordingState == RecordingState.recording) {
                          await audioRecorderService.stopRecording();
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                              content: Text("Audio recording saved!"),
                              duration: Duration(seconds: 2),
                            ),
                          );
                        }
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.green,
                        padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 40),
                ElevatedButton(
                  onPressed: () {

                  },
                  child: const Text('Go to Player'),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}