From 2599cb1f6eb2d4a978b65642e5c30b5dcb0dc865 Mon Sep 17 00:00:00 2001 From: Rodrigo Boratto Date: Tue, 26 Jul 2022 02:29:41 -0300 Subject: [PATCH] Restart proxy in case of -1004 error --- just_audio/lib/just_audio.dart | 43 ++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index c877ee2dc..ffa1b0d49 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -837,6 +837,16 @@ class AudioPlayer { return duration; } on PlatformException catch (e) { try { + // cant connect to servers + if (e.code == "-1004" && source is LockCachingAudioSource) { + // proxy is offline + try { + await _proxy._server.close(force: true); + } catch (_) { + // ignore err + } + await _proxy.start(); + } throw PlayerException(int.parse(e.code), e.message); } on FormatException catch (_) { if (e.code == 'abort') { @@ -1995,18 +2005,28 @@ class _ProxyHttpServer { /// Starts the server. Future start() async { _running = true; - _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); - _server.listen((request) async { - if (request.method == 'GET') { - final uriPath = _requestKey(request.uri); - final handler = _handlerMap[uriPath]!; - handler(this, request); - } - }, onDone: () { - _running = false; - }, onError: (Object e, StackTrace st) { + try { + _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + _server.listen((request) async { + if (request.method == 'GET') { + final uriPath = _requestKey(request.uri); + final handler = _handlerMap[uriPath]!; + handler(this, request); + } + }, onDone: () { + _running = false; + }, onError: (Object e, StackTrace st) async { + _running = false; + try { + await _server.close(force: true); + } catch (_) { + // ignore + } + }, cancelOnError: true); + } catch (_) { + // ignore _running = false; - }); + } } /// Stops the server @@ -2813,7 +2833,6 @@ class LockCachingAudioSource extends StreamAudioSource { _downloadProgressSubject.add(percentProgress / 100); } } - _progress = 0; subscription = response.listen((data) async { _progress += data.length;