Open aanchalMicro opened 11 months ago
这是来自QQ邮箱的自动回复邮件。 您好,您的邮件我已收到,我将尽快给您回复。
Hey @aanchalMicro, I'd be happy to help you with adding the functionality for seeking forward and backward in a sound wave recorder! Below is an explanation of how you can implement these features in your Flutter app.
First, let's address the forward functionality: Seek Forward Functionality: void forwardAudioByDuration(Duration duration) { if (_isPlayingAudio) { final currentPosition = playerController.currentPosition; final targetPosition = currentPosition + duration; final maxDuration = playerController.duration;
if (targetPosition < maxDuration) { playerController.seekTo(targetPosition); } else { playerController.seekTo(maxDuration); } } }
In this code, forwardAudioByDuration takes a Duration as an argument and calculates the target position for seeking forward. It then checks if the target position is within the bounds of the audio duration and seeks to that position using playerController.seekTo.
Forward Button to Seek: GestureDetector( onTap: () { forwardAudioByDuration(Duration(seconds: 5)); // Seek forward by 5 seconds }, child: Image.asset( "assets/images/forward.png", height: SizeConfig.screenHeight * 0.08, ), ),
Seek Backward Functionality: void backwardAudioByDuration(Duration duration) { if (_isPlayingAudio) { final currentPosition = playerController.currentPosition; final targetPosition = currentPosition - duration;
if (targetPosition > Duration.zero) { playerController.seekTo(targetPosition); } else { playerController.seekTo(Duration.zero); } } } Similar to the forward functionality, backwardAudioByDuration calculates the target position for seeking backward and ensures it's within bounds before seeking using playerController.seekTo.
Backward Button to Seek: GestureDetector( onTap: () { backwardAudioByDuration(Duration(seconds: 5)); // Seek backward by 5 seconds }, child: Image.asset( "assets/images/backward.png", height: SizeConfig.screenHeight * 0.08, ), ),
Make sure you adjust the Duration values and UI components according to your design and requirements. With these implementations, your users should be able to seek forward and backward within the audio playback.
Remember to handle state changes appropriately, especially if you are using a state management solution like Provider.
Working in flutter need solution for recording sound with wave form and forward it by 15 seconds , I tried many solutions but they were not helpful like using duration custom or custom..... also used this package audio_waveforms but didnt worked if anyone could help please respond asap
import 'dart:io';
import 'package:audio_waveforms/audio_waveforms.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; import 'package:smilestone/Audio_Screen/AudioRecordingProvider.dart'; import 'package:smilestone/Utils/sizeConfig.dart';
import '../Utils/utils.dart';
class AudioRecording extends StatefulWidget { const AudioRecording({Key key}) : super(key: key);
@override State createState() => _AudioRecordingState();
}
class _AudioRecordingState extends State {
RecorderController recorderController;
PlayerController playerController;
bool _isPlayingAudio = false;
bool _isPlayIcon = true;
bool closeIcon = false;
bool yesDialog = false;
String audioFilePath;
TextEditingController saveController=TextEditingController();
bool listOfSound=false;
Duration currentPosition = Duration.zero;
Duration _position = Duration.zero;
Duration _duration = Duration.zero;
var filePath;
void toggleBtn() { if (_isPlayingAudio == false) { _isPlayingAudio = !_isPlayingAudio; _startRecording(); } if (_isPlayIcon == false) { _isPlayIcon = !_isPlayIcon; } setState(() {}); }
void toggleIconBtn() { _isPlayIcon = !_isPlayIcon; _pauseAudio(); if(_isPlayIcon==true){ _pauseAudio();
}
void _initializeController() { recorderController = RecorderController() ..androidEncoder = AndroidEncoder.aac ..androidOutputFormat = AndroidOutputFormat.mpeg4 ..iosEncoder = IosEncoder.kAudioFormatMPEG4AAC ..sampleRate = 16000;
}
void _startRecording() async { try { if (!recorderController.isRecording) { await recorderController.record(); } else { final path = await recorderController.stop(); _isPlayingAudio = true;
}
void forwardAudioBy15Seconds() async { if (_isPlayingAudio) { var duration = playerController.getDuration(DurationType.current); print("duration===>"+duration.toString()); playerController.onPlayerStateChanged.skip(15000); // final Duration newPosition = currentPosition + Duration(seconds: 15); // playerController.seekTo(newPosition.inSeconds); // print("object"+newPosition.toString());
}
Future _saveAudio() async {
final appDir = await getExternalStorageDirectory();
final folderPath = '${appDir.parent.path}/audiofolder';
final folder = Directory(folderPath);
if (!(await folder.exists())) {
await folder.create(recursive: true);
}
var audioFileName = "";
if (saveController.text != null && saveController.text.isNotEmpty) {
audioFileName = '${saveController.text}.wav';
} else {
audioFileName = 'audio${DateTime.now().millisecondsSinceEpoch}.wav';
}
print("audioFileName"+audioFileName);
filePath = '$folderPath/$audioFileName';
print("file path"+filePath);
final recordedAudioFile = File(audioFilePath);
final copiedFile = await recordedAudioFile.copy(filePath);
}
void _pauseAudio() async { if (_isPlayIcon) { playerController.pausePlayer(); _isPlayingAudio=true; } else { playerController.startPlayer(finishMode: FinishMode.loop); } _isPlayIcon = !_isPlayIcon; setState(() {}); }
@override void initState() { super.initState();
}
@override void dispose() { super.dispose(); recorderController.dispose(); playerController.dispose(); }
Widget SelectListOfAudio(){ return Container( width: SizeConfig.screenWidth 0.11, height: SizeConfig.screenHeight 0.18, decoration: BoxDecoration( // border: Border.all(width: 0.5), color: Color.fromRGBO(0, 0, 0, 0.7), borderRadius: BorderRadius.circular(36), shape: BoxShape.rectangle, ), child: Column( children: [ SizedBox(height: SizeConfig.screenHeight 0.01,), GestureDetector( child: Image.asset( "assets/images/audioImage.png", height: SizeConfig.screenHeight 0.045, ), ), SizedBox(height: SizeConfig.screenHeight 0.01,), GestureDetector( child: Image.asset( "assets/images/childImage.png", height: SizeConfig.screenHeight 0.045, ), ), SizedBox(height: SizeConfig.screenHeight 0.01,), GestureDetector( child: Image.asset( "assets/images/child_animalImg.png", height: SizeConfig.screenHeight 0.045, width: SizeConfig.screenWidth *0.088, ), ) ], ), ); }
@override Widget build(BuildContext context) { SizeConfig().init(context); return Scaffold( body: Container( child: SingleChildScrollView( child: Column( children: [ Container( margin: EdgeInsets.only(top: SizeConfig.screenHeight 0.055), alignment: Alignment.topCenter, child: Image.asset( "assets/images/smileStone.png", width: SizeConfig.screenWidth 0.4, )),
// Seek forward by a specific duration // Duration currentPosition = playerController.getDuration(DurationType.current); // Duration forwardDuration = Duration(seconds: 5); // Seek forward by 5 seconds // Duration targetPosition = currentPosition + forwardDuration; // // if (targetPosition > playerController.) { // // Adjust target position to the maximum duration if it exceeds the duration // targetPosition = playerController.duration; // } // // await playerController.seekTo(targetPosition); var duration = playerController.getDuration(DurationType.current); print("duration===>"+duration.toString());
} }