2

I have a local dart server, serving music and videos. I tested it with android device and a flutter application and it worked with just_audio and video_player packages after adding this line in AndriodManifest.xml:

android:usesCleartextTraffic="true"

but when I am trying to run the same app on web I get an error saying:

(4) Failed to load URL

and when i tried to run another online video and audio it worked on the flutter web app

here is some snippets of my dart server code:

    request.response.headers.add('Access-Control-Allow-Origin', '*');
    request.response.headers.add('Access-Control-Allow-Methods', '*');
    request.response.headers.add('Access-Control-Allow-Headers', '*');

and

  File file = File(audioPath);
  int length = await file.length();

  String range = req.headers.value('range') ?? 'bytes=0-';
  List<String> parts = range.split('=');
  List<String> positions = parts[1].split('-');
  int start = int.parse(positions[0]);
  int end = positions.length < 2 || int.tryParse(positions[1]) == null
      ? length
      : int.parse(positions[1]);
  String? mime = lookupMimeType(audioPath);

  response.statusCode = HttpStatus.partialContent;
  response.headers
    ..contentType = ContentType.parse(mime ?? 'audio/mpeg')
    ..contentLength = end - start
    ..add('Accept-Ranges', 'bytes')
    ..add('Content-Range', 'bytes $start-$end/$length');
  file.openRead(start, end).pipe(req.response);

so I think the solution to this issue is to do similar thing like this but in web:

android:usesCleartextTraffic="true"

and one more thing when I checked for request.headers on the dart server, I didn't find the headers that I sent with the request from the frontend:

user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
connection: keep-alive
accept: */*
accept-language: en-US,en;q=0.9
range: bytes=0-
accept-encoding: identity;q=1, *;q=0
host: 192.168.1.39:49359
referer: http://localhost:62278/
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Amr Hassan
  • 160
  • 8

0 Answers0