[bug] feedFromStream() "RangeError.checkValidRange"
See original GitHub issueI need Help for :
- Using the API
Here is my question :
Hello everyone! I have a problem - audio streaming is not working. This is the part of the project that organizes real-time communication between users of the same group via a socket. So far I have removed the unnecessary part of the code to make it more convenient. For now, I’m just trying to make streaming work. I would be very grateful if you could help steer me in the right direction. Perhaps I did not take into account some things. This is my first flutter project and I have been writing it for a long time, a lot of things have already been implemented, but now I ran into a small problem.
I will make a description of the classes:
SoundPlayer is player; MultiSoundPlayer is players controller for several players; ActivityVoiceCommunicationIOService is a service that will later organize the playback of sound from other users.
The problem arises when i do feedHim():
2021-01-17 16:37:51.402 11278-11278/com.example.project_name E/FlautoPlayer: Feed() is not available
2021-01-17 16:37:51.404 11278-11311/com.example.project_name E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError (start): Invalid value: Not in inclusive range 0..4096: -1
#0 RangeError.checkValidRange (dart:core/errors.dart:352:7)
#1 _TypedIntListMixin.sublist (dart:typed_data-patch/typed_data_patch.dart:462:31)
#2 FlutterSoundPlayer.feedFromStream (package:flutter_sound/public/flutter_sound_player.dart:983:35)
<asynchronous suspension>
#3 FoodData.exec (package:flutter_sound/public/tau.dart:112:58)
#4 FlutterSoundPlayer.startPlayerFromStream.<anonymous closure>.<anonymous closure> (package:flutter_sound/public/flutter_sound_player.dart:947:44)
#5 _rootRunUnary (dart:async/zone.dart:1198:47)
#6 _ZoneDelegate.runUnary (dart:async/zone.dart:765:19)
#7 AsyncAction._runUnary (package:mobx/src/api/async/async_action.dart:58:29)
#8 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
#9 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
#10 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
#11 _DelayedData.perform (dart:async/stream_impl.dart:611:14)
#12 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
#13 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
#14 _rootRun (dart:async/zone.dart:1182:47)
#15 _ZoneDelegate.run (dart:async/zone.dart:758:19)
#16 AsyncAction._run (package:mobx/src/api/async/async_action.dart:44:29)
#17 _CustomZone.run (dart:async/zone.dart:1093:19)
#18 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#19 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
#20 _rootRun (dart:async/zone.dart:1190:13)
#21 _ZoneDelegate.run (dart:async/zone.dart:758:19)
#22 AsyncAction._run (package:mobx/src/api/async/async_action.dart:44:29)
#23 _CustomZone.run (dart:async/zone.dart:1093:19)
#24 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#25 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
#26 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:project_name/dto/voice-part.dto.dart';
import 'package:project_name/main.dart';
import 'package:project_name/services/activity-socket.service.dart';
import 'package:project_name/services/auth.dart';
int blockSize = 4096;
class SoundPlayer {
FlutterSoundPlayer _mPlayer = FlutterSoundPlayer();
bool _mPlayerIsInited = false;
StreamController<Uint8List> streamController = StreamController();
SoundPlayer();
init() async {
await _mPlayer.openAudioSession();
await _mPlayer.startPlayerFromStream(codec: Codec.pcm16, numChannels: 1, sampleRate: 44000);
_mPlayerIsInited = true;
streamController.stream.listen((Uint8List buffer) async {
return await feedHim(buffer);
});
}
Future<void> feedHim(Uint8List buffer) async {
var lnData = 0;
var totalLength = buffer.length;
while (totalLength > 0 && !_mPlayer.isStopped) {
var bsize = totalLength > blockSize ? blockSize : totalLength;
await _mPlayer.feedFromStream(buffer.sublist(lnData, lnData + bsize)); // await !!!!
lnData += bsize;
totalLength -= bsize;
}
}
putSound(Uint8List buffer) async {
assert(_mPlayerIsInited);
streamController.add(buffer);
}
dispose() {
stopPlayer();
_mPlayer.closeAudioSession();
_mPlayer = null;
}
Future<void> stopPlayer() async {
await _mPlayer.stopPlayer();
}
}
class MultiSoundPlayer {
Map<String, SoundPlayer> _players = Map();
playSound(String name, Uint8List buffer) async {
if (!_players.containsKey(name)) {
_players[name] = new SoundPlayer();
await _players[name].init();
}
return _players[name].putSound(buffer);
}
dispose() {
_players.forEach((key, value) { value.dispose(); });
}
}
class ActivityVoiceCommunicationIOService {
Uint8List _buffer = Uint8List(BUF_SIZE * 2);
MultiSoundPlayer player = MultiSoundPlayer();
static final int BUF_SIZE = 65536;
ActivityVoiceCommunicationIOService();
int realLength = 0;
// Will be addToSendQueue but now local test
playSoundInPlayer(Uint8List data) async {
if (realLength + data.length <= _buffer.length) {
this._buffer.setAll(realLength, data);
realLength += data.length;
}
if (realLength >= ActivityVoiceCommunicationIOService.BUF_SIZE) {
if (realLength > 0) {
player.playSound('default', _buffer.sublist(0, realLength));
}
this.realLength = 0;
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:11
Top GitHub Comments
Hi @sovaz1997 ,
Flutter Sound 7.5.3 is released. It fixes a major regression for Android that I introduced in 7.5.1 on feed()
I introduce from time to time some major regressions to be sure that there is someone who uses my work 😉 .
Sorry for that. And sorry for the delay. Good luck for your personal project
Thank you so much! Rest more, this is important. I will write to you by e-mail