AssetsAudioPlayer the operation could not be completed iOS - flutter

I'm using Flutter Sound Recorder as recorder for my audio files and I'm trying to open the audio file with AssetsAudioPlayer but I'm getting this error:
url: file:///var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav
"playback failed"
2022-06-23 23:45:04.546002-0300 Runner[412:13249] flutter: PlatformException(PLAY_ERROR, Cannot play /var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav, The operation could not be completed, null)
2022-06-23 23:45:04.546905-0300 Runner[412:13249] [VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: PlatformException(PLAY_ERROR, Cannot play /var/mobile/Containers/Data/Application/10E4FD0F-E4F2-491E-A1A0-360A3E294217/Library/Caches/audios/1656038702016.wav, The operation could not be completed, null)
I've tried to use Audio Players but I got an error too
"iOS => call setVolume, playerId 57c0b5cc-3c75-4f8f-bf99-ad8ddbcb7709"
"iOS => call setUrl, playerId 57c0b5cc-3c75-4f8f-bf99-ad8ddbcb7709"
""
2022-06-23 18:27:00.168194-0300 Runner[16264:695114] flutter: AVPlayerItem.Status.failed
Any idea of what can I Do?
My recording settings:
Future<void> _onRecordButtonPressed() async {
if(_isRecording){
await _myRecorder?.stopRecorder();
_isRecording = false;
_isRecorded = true;
}else{
_isRecorded = false;
_isRecording = true;
await _startRecording();
}
setState((){
});
}
My AssetsAudioPlayer settings:
_onPlayButtonPressed() async {
if(!_isPlaying) {
_isPlaying = true;
await audioPlayer2.onPlayerError.listen((event) {
print(event);
});
await _assetsAudioPlayer.open(Audio.file(FilePathAndroidBitmap));
}else{
audioPlayer2.pause();
_isPlaying = false;
}
setState((){});
}

Related

Flutter_sound doesn't work at the first time but after recording voice on iOS

I'm building a flutter app and inside I use flutter_sound for voice recording and playing.
It works totally fine with Android devices but not with iOS device.
Right after building the app, I play a sound fetched from firebase but there was no sound at all. However, after I record a new voice, the previously fetched sound can play properly.
This is the code for playing sound
void startPlay() {
isPlaying = true;
_player = FlutterSoundPlayer();
_player!.openPlayer();
_player!.startPlayer(
fromDataBuffer: widget.bodyBytes,
codec: Codec.pcm16,
sampleRate: 48000,
whenFinished: () {
isPlaying = false;
if(mounted) {
setState(() {});
}
});
setState(() {});
}
This is the code for recording
Future<bool> record() async {
var status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
status = await Permission.microphone.request();
return false;
}
recordingDataController = StreamController<Food>.broadcast();
// write to data
_sink = File(_tempFilePath!).openWrite();
recordingDataSubscriptor = recordingDataController!.stream.listen((buffer) {
if (buffer is FoodData) {
_sink.add(buffer.data!);
}
});
await _recorder!.openRecorder();
await _recorder!.startRecorder(
toStream: recordingDataController!.sink,
codec: Codec.pcm16,
numChannels: 1,
sampleRate: 48000,
);
return true;
}
Please help me, Love you all <3

Switching stream with Flutter WebRTC seems to fail

I'm trying to switch from the front to back camera using Flutter WebRTC but cannot get it working.
I have the following
// Stop the current stream and remove the tracks
await Future.forEach(videoStream!.getVideoTracks(), (MediaStreamTrack? track) async {
if (track != null) {
try {
await track.stop();
await videoStream!.removeTrack(track);
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
});
videoStream!.getVideoTracks().forEach((track) {
track.stop();
videoStream!.removeTrack(track, removeFromNative: true);
});
final mediaConstraints = {
'audio': false, // NO need to capture audio again
'video': {
'deviceId': videoInputDeviceId,
}
};
MediaStream newStream = await navigator.mediaDevices.getUserMedia(mediaConstraints);
final newTrack = newStream.getVideoTracks()[0];
await videoStream!.addTrack(newTrack, addToNative: true);
Getting the following errors if I place try catch around them
flutter: PlatformException(mediaStreamRemoveTrack: Track is nil, null, null, null)
flutter: !--- Event: Failed to enable webcam
flutter: Concurrent modification during iteration: Instance(length:0) of '_GrowableList'.

Null check operator used on a null value- futter chat

I'm creating a chat app with sending voice message. now I'm getting this error in my code. appreciate your help on this. I HAVE INSERTED FOLLOWING CODE ANS THEN ERROR APPEARS. Cant find a exact file error occurring.
Null check operator used on a null value
class SoundRecorder {
FlutterSoundRecorder? _audioRecorder;
bool _isRecorderInitialised = false;
bool get isRecording => _audioRecorder!.isRecording;
Future init() async {
_audioRecorder = FlutterSoundRecorder();
await _audioRecorder?.openAudioSession(); //start recording
//asking permisson
final status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
throw RecordingPermissionException("Microphone permission");
}
await _audioRecorder!.openAudioSession();
_isRecorderInitialised = true;
}
void dispose() {
if (!_isRecorderInitialised) return;
_audioRecorder!.closeAudioSession();
_audioRecorder = null;
_isRecorderInitialised = false;
}
Future _record() async {
if (!_isRecorderInitialised) return;
await _audioRecorder!.startRecorder(toFile: pathToSaveAudio);
}
Future _stop() async {
if (!_isRecorderInitialised) return;
await _audioRecorder!.stopRecorder();
}
Future toggleRecording() async {
if (_audioRecorder!.isStopped) {
await _record();
} else {
await _stop();
}
}
}
Looks like at some point you use the ! operator to assert that _audioRecorder isn't null but it actually is. From the stack, I think this would be from the isRecording getter.
A simple fix to this would be to make the getter bool get isRecording => _audioRecorder?.isRecording ?? false, since if _audioRecorder is null, then you can't be recording, right?

flutter sounds unable to play audio file

I am using flutter_sounds package to play audio file.
My audio file exists and is valid file.
For some unknown reason below code is not working and unable to play the audio.
FlutterSoundPlayer _mPlayer = FlutterSoundPlayer();
try {
var t = await _mPlayer.openAudioSession();
print('after open');
print('_mPlayerIsInited');
setState(() async {
_isPlayerToucher = true;
_mPlayerIsInited = true;
_mPlayerisPaused = false;
});
} catch (e) {
print('ERROR');
print(e.toString());
}
await _mPlayer.startPlayer(
fromURI: widget.content,
whenFinished: () async {
await _mPlayer.stopPlayer();
_mPlayer.closeAudioSession();
setState(() {
_mPlayerIsInited = false;
});
});
I am unable to see the print 'after open'
i see below in console.
I/flutter ( 7834): FS:---> openAudioSession
I recommend you to use another package audioplayers
AudioCache _audioCache;
_audioCache = AudioCache(
prefix: "audio/",
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
_audioCache.play('open-up.mp3');

How to Stop Video Recording After some Seconds?

I'm recording video using image_picker. I want to stop that recording after 20seconds. Is there any way to stop recording after 20seconds.
_pickVideoFromCamera() async {
File video = await ImagePicker.pickVideo(source: ImageSource.camera);
_cameraVideo = video;
_cameraVideoPlayerController = VideoPlayerController.file(_cameraVideo)
..initialize().then((_) {
_cameraVideoPlayerController.play();
setState(() {});
});
}
You can use Future.delayed
_pickVideoFromCamera() async {
File video = await ImagePicker.pickVideo(source: ImageSource.camera);
_cameraVideo = video;
_cameraVideoPlayerController = VideoPlayerController.file(_cameraVideo)
..initialize().then((_) {
_cameraVideoPlayerController.play();
setState(() {});
});
Future.delayed(Duration(seconds: 20)).then((_) { // camera stop process });
}