我试图在 macOS 中录制音频,但发现生成的音频文件有问题,我的意思是当我尝试播放录制的文件时,听不到任何声音,就像是空的一样。
我用来录制音频的包:record
用于播放音频:audioplayers
完整代码:
main.dart
:
import 'package:flutter/material.dart';
import 'recorder/audio_recorder_sample.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const AudioRecorderSample());
}
}
audio_recorder_sample.dart
:
import 'package:flutter/material.dart';
import 'package:flutter_audio_rec/recorder/widgets/recorder.dart';
import '../player/linear_audio_player.dart';
class AudioRecorderSample extends StatefulWidget {
const AudioRecorderSample({super.key});
@override
State<AudioRecorderSample> createState() => _AudioRecorderSampleState();
}
class _AudioRecorderSampleState extends State<AudioRecorderSample> {
String? recordedAudioPath;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (recordedAudioPath?.isNotEmpty ?? false) ...[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: LinearAudioPlayer(
source: recordedAudioPath ?? '',
onDelete: () {
setState(() {
recordedAudioPath = null;
});
}),
),
const SizedBox(
height: 10,
),
],
Recorder(onStop: (path) {
debugPrint("RECORDED AUDIO FILE====================>$path");
setState(() {
recordedAudioPath = path;
});
})
],
),
);
}
}
recorder.dart
:
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:record/record.dart';
import 'package:path_provider/path_provider.dart';
class Recorder extends StatefulWidget {
final void Function(String path) onStop;
const Recorder({super.key, required this.onStop});
@override
State<Recorder> createState() => _RecorderState();
}
class _RecorderState extends State<Recorder> {
int _recordDuration = 0;
Timer? _timer;
late final AudioRecorder _audioRecorder;
StreamSubscription<RecordState>? _recordSub;
RecordState _recordState = RecordState.stop;
StreamSubscription<Amplitude>? _amplitudeSub;
Amplitude? _amplitude;
late Directory appDirectory;
String? path;
bool recordingDone = false;
bool isRecording = false;
void getDir() async {
appDirectory = await getApplicationDocumentsDirectory();
path =
"${appDirectory.path}/${DateTime.now().millisecondsSinceEpoch}.mp3"; //recording
}
@override
void initState() {
getDir();
_audioRecorder = AudioRecorder();
_recordSub = _audioRecorder.onStateChanged().listen((recordState) {
_updateRecordState(recordState);
});
_amplitudeSub = _audioRecorder
.onAmplitudeChanged(const Duration(milliseconds: 300))
.listen((amp) {
setState(() => _amplitude = amp);
});
super.initState();
}
Future<void> _start() async {
setState(() => recordingDone = false);
try {
if (await _audioRecorder.hasPermission()) {
const encoder = AudioEncoder.aacLc;
if (!await _isEncoderSupported(encoder)) {
return;
}
final devs = await _audioRecorder.listInputDevices();
debugPrint(devs.toString());
const config = RecordConfig(encoder: encoder, numChannels: 1);
_audioRecorder.start(config, path: path ?? '');
_recordDuration = 0;
_startTimer();
}
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
Future<void> _stop() async {
path = await _audioRecorder.stop();
if (path != null) {
widget.onStop(path!);
setState(() {
recordingDone = true;
});
}
}
Future<void> _pause() => _audioRecorder.pause();
Future<void> _resume() => _audioRecorder.resume();
void _updateRecordState(RecordState recordState) {
setState(() => _recordState = recordState);
switch (recordState) {
case RecordState.pause:
_timer?.cancel();
break;
case RecordState.record:
_startTimer();
break;
case RecordState.stop:
_timer?.cancel();
_recordDuration = 0;
break;
}
}
Future<bool> _isEncoderSupported(AudioEncoder encoder) async {
final isSupported = await _audioRecorder.isEncoderSupported(
encoder,
);
if (!isSupported) {
debugPrint('${encoder.name} is not supported on this platform.');
debugPrint('Supported encoders are:');
for (final e in AudioEncoder.values) {
if (await _audioRecorder.isEncoderSupported(e)) {
debugPrint('- ${encoder.name}');
}
}
}
return isSupported;
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildRecordStopControl(),
const SizedBox(width: 20),
_buildPauseResumeControl(),
const SizedBox(width: 20),
_buildText(),
],
),
if (_amplitude != null) ...[
const SizedBox(height: 40),
Text('Current: ${_amplitude?.current ?? 0.0}'),
Text('Max: ${_amplitude?.max ?? 0.0}'),
],
],
);
}
@override
void dispose() {
_timer?.cancel();
_recordSub?.cancel();
_amplitudeSub?.cancel();
_audioRecorder.dispose();
super.dispose();
}
Widget _buildRecordStopControl() {
late Icon icon;
late Color color;
if (_recordState != RecordState.stop) {
icon = const Icon(Icons.stop, color: Colors.red, size: 30);
color = Colors.red.withOpacity(0.1);
} else {
final theme = Theme.of(context);
icon = const Icon(Icons.mic, color: Colors.black, size: 30);
color = theme.primaryColor.withOpacity(0.1);
}
return ClipOval(
child: Material(
color: color,
child: InkWell(
child: SizedBox(width: 56, height: 56, child: icon),
onTap: () {
(_recordState != RecordState.stop) ? _stop() : _start();
},
),
),
);
}
Widget _buildPauseResumeControl() {
if (_recordState == RecordState.stop) {
return const SizedBox.shrink();
}
late Icon icon;
if (_recordState == RecordState.record) {
icon = const Icon(Icons.pause, color: Colors.red, size: 30);
} else {
icon = const Icon(Icons.play_arrow, color: Colors.red, size: 30);
}
return ClipOval(
child: Material(
// color: color,
child: InkWell(
child: SizedBox(width: 56, height: 56, child: icon),
onTap: () {
(_recordState == RecordState.pause) ? _resume() : _pause();
},
),
),
);
}
Widget _buildText() {
if (_recordState != RecordState.stop) {
return _buildTimer();
}
return const Text("Waiting to record");
}
Widget _buildTimer() {
final String minutes = _formatNumber(_recordDuration ~/ 60);
final String seconds = _formatNumber(_recordDuration % 60);
return Text(
'$minutes : $seconds',
style: const TextStyle(color: Colors.red),
);
}
String _formatNumber(int number) {
String numberStr = number.toString();
if (number < 10) {
numberStr = '0$numberStr';
}
return numberStr;
}
void _startTimer() {
_timer?.cancel();
_timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
setState(() => _recordDuration++);
});
}
}
linear_audio_player.dart
:
import 'dart:async';
import 'package:audioplayers/audioplayers.dart' as ap;
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class LinearAudioPlayer extends StatefulWidget {
final String source;
final void Function()? onDelete;
const LinearAudioPlayer({
super.key,
required this.source,
required this.onDelete,
});
@override
LinearAudioPlayerState createState() => LinearAudioPlayerState();
}
class LinearAudioPlayerState extends State<LinearAudioPlayer> {
static const double _controlSize = 56;
final _audioPlayer = ap.AudioPlayer();
// final _audioPlayer = ap.AudioPlayer()..setReleaseMode(ReleaseMode.stop);
late StreamSubscription<void> _playerStateChangedSubscription;
late StreamSubscription<Duration?> _durationChangedSubscription;
late StreamSubscription<Duration> _positionChangedSubscription;
Duration? _position;
Duration? _duration;
@override
void initState() {
_init();
super.initState();
}
_init() async {
_playerStateChangedSubscription =
_audioPlayer.onPlayerComplete.listen((state) async {
await stop();
});
_positionChangedSubscription = _audioPlayer.onPositionChanged.listen(
(position) => setState(() {
_position = position;
}),
);
_durationChangedSubscription = _audioPlayer.onDurationChanged.listen(
(duration) => setState(() {
_duration = duration;
}),
);
await _audioPlayer.setSource(_source);
}
@override
void dispose() {
_playerStateChangedSubscription.cancel();
_positionChangedSubscription.cancel();
_durationChangedSubscription.cancel();
_audioPlayer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_buildControl(),
_buildSlider(constraints.maxWidth),
Text('${_duration ?? 0.0}'),
],
),
//
],
);
},
);
}
Widget _buildControl() {
Icon icon;
Icon deleteIcon = const Icon(Icons.delete, color: Colors.black, size: 30);
if (_audioPlayer.state == ap.PlayerState.playing) {
icon = const Icon(Icons.pause, color: Colors.black, size: 30);
} else {
icon = const Icon(Icons.play_arrow, color: Colors.black, size: 30);
}
return Row(
children: [
InkWell(
onTap: widget.onDelete,
child: SizedBox(
width: _controlSize, height: _controlSize, child: deleteIcon),
),
InkWell(
child:
SizedBox(width: _controlSize, height: _controlSize, child: icon),
onTap: () {
if (_audioPlayer.state == ap.PlayerState.playing) {
pause();
} else {
play();
}
},
),
],
);
}
Widget _buildSlider(double widgetWidth) {
bool canSetValue = false;
final duration = _duration;
final position = _position;
if (duration != null && position != null) {
canSetValue = position.inMilliseconds > 0;
canSetValue &= position.inMilliseconds < duration.inMilliseconds;
}
return Expanded(
// width: width,
child: Slider(
activeColor: Colors.black,
inactiveColor: Colors.grey,
onChanged: (v) {
if (duration != null) {
final position = v * duration.inMilliseconds;
_audioPlayer.seek(Duration(milliseconds: position.round()));
}
},
value: canSetValue && duration != null && position != null
? position.inMilliseconds / duration.inMilliseconds
: 0.0,
),
);
}
Future<void> play() async =>
await _audioPlayer.play(_source, ctx: ap.AudioContext());
Future<void> pause() async {
await _audioPlayer.pause();
setState(() {});
}
Future<void> stop() async {
await _audioPlayer.stop();
setState(() {});
}
Source get _source => kIsWeb
? ap.UrlSource(widget.source)
: ap.DeviceFileSource(
widget.source,
);
}