Skip to content

Commit

Permalink
working on the audio feature
Browse files Browse the repository at this point in the history
  • Loading branch information
njogubless committed Feb 8, 2025
1 parent 76d71e1 commit a45f184
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 161 deletions.
35 changes: 35 additions & 0 deletions lib/features/audio/data/repositories/audio_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// import 'dart:io';
// import 'package:flutter_sound/flutter_sound.dart';
// import 'package:path_provider/path_provider.dart';

// class AudioRepository {
// final FlutterSoundRecorder _recorder = FlutterSoundRecorder();
// final FlutterSoundPlayer _player = FlutterSoundPlayer();
// String? filePath;

// Future<void> startRecording() async {
// Directory tempDir = await getApplicationDocumentsDirectory();
// filePath = "${tempDir.path}/audio_${DateTime.now().millisecondsSinceEpoch}.aac";

// await _recorder.openRecorder();
// await _recorder.startRecorder(toFile: filePath);
// }

// Future<void> stopRecording() async {
// await _recorder.stopRecorder();
// }

// Future<List<File>> getAllRecordings() async {
// Directory dir = await getApplicationDocumentsDirectory();
// return dir.listSync().whereType<File>().toList();
// }

// Future<void> playAudio(File file) async {
// await _player.openPlayer();
// await _player.startPlayer(fromURI: file.path);
// }

// Future<void> stopPlayback() async {
// await _player.stopPlayer();
// }
// }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// lib/features/audio/presentation/providers/audio_recorder_provider.dart
import 'package:devotion/features/audio/presentation/screens/audio_screen.dart';
import 'package:devotion/features/audio/presentation/screens/audio_recoding_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:record/record.dart';
import 'dart:async';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// lib/features/audio/presentation/providers/audio_recorder_provider.dart
import 'package:devotion/features/audio/presentation/screens/audio_screen.dart';
import 'package:devotion/features/audio/presentation/screens/audio_recoding_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:record/record.dart';
import 'dart:async';
Expand Down
70 changes: 70 additions & 0 deletions lib/features/audio/presentation/screens/audio_list_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// lib/features/audio/presentation/pages/audio_list_page.dart
class AudioListPage extends ConsumerWidget {
const AudioListPage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('Recorded Sermons'),
backgroundColor: Theme.of(context).primaryColor,
actions: [
IconButton(
icon: const Icon(Icons.add),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => const RecordAudioPage()),
),
),
],
),
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('Devotion').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Center(child: Text('Something went wrong'));
}

if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}

final recordings = snapshot.data?.docs ?? [];

return ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: recordings.length,
itemBuilder: (context, index) {
final recording = AudioFile.fromJson(
recordings[index].data() as Map<String, dynamic>
);

return Card(
margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
child: ListTile(
leading: const CircleAvatar(
child: Icon(Icons.audio_file),
),
title: Text(recording.title),
subtitle: Text(recording.scripture ?? ''),
trailing: IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: () {
// Navigate to audio player page
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => AudioPlayerPage(audioFile: recording),
),
);
},
),
),
);
},
);
},
),
);
}
}
96 changes: 96 additions & 0 deletions lib/features/audio/presentation/screens/audio_player_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import 'package:devotion/features/audio/data/models/audio_model.dart';
import 'package:devotion/features/audio/presentation/providers/audio_player_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class AudioPlayerPage extends ConsumerStatefulWidget {
final AudioFile audioFile;

const AudioPlayerPage({required this.audioFile, super.key});

@override
_AudioPlayerPageState createState() => _AudioPlayerPageState();
}

class _AudioPlayerPageState extends ConsumerState<AudioPlayerPage> {
@override
Widget build(BuildContext context) {
final playerState = ref.watch(audioPlayerProvider);

return Scaffold(
appBar: AppBar(
title: Text(widget.audioFile.title),
backgroundColor: Theme.of(context).primaryColor,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Card(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
widget.audioFile.title,
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
widget.audioFile.scripture ?? '',
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
Slider(
value: playerState.position.inSeconds.toDouble(),
max: playerState.duration.inSeconds.toDouble(),
onChanged: (value) {
ref.read(audioPlayerProvider.notifier)
.seekTo(Duration(seconds: value.toInt()));
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(_formatDuration(playerState.position)),
Text(_formatDuration(playerState.duration)),
],
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.replay_10),
onPressed: () => ref.read(audioPlayerProvider.notifier)
.seekTo(playerState.position - const Duration(seconds: 10)),
),
FloatingActionButton(
onPressed: () => playerState.isPlaying
? ref.read(audioPlayerProvider.notifier).pause()
: ref.read(audioPlayerProvider.notifier).play(),
child: Icon(
playerState.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
IconButton(
icon: const Icon(Icons.forward_10),
onPressed: () => ref.read(audioPlayerProvider.notifier)
.seekTo(playerState.position + const Duration(seconds: 10)),
),
],
),
],
),
),
),
],
),
),
);
}
}
Loading

0 comments on commit a45f184

Please sign in to comment.