omar hatem
omar hatem

Reputation: 1989

Flutter video player throws an exception on iOS only

Flutter plugin "video_player" works fine on android but when testing on iOS it throws the following exception:

enter image description here

PS: I have tested the app on iPhone XS Max and iPhone 11 Pro Max (real devices)

Also, I have tried different video formats like (mp4, flv, webm) and nothing works

I looked through all the issues opened in the video_player plugin and none had this exception.

Flutter Version: 1.12.13+hotfix.9 Dart Version: 2.7.2

Any help would be appreciated. Thanks in advance

Upvotes: 3

Views: 6404

Answers (2)

Arokip
Arokip

Reputation: 91

I had the same issue. Content range header returned from API is needed for iOS to work.

The format of the header: Content-Range: bytes 0-158008373/158008374

For fixing this bug this gist was used as an inspiration: https://gist.github.com/codler/3906826

EDIT: as suggested I am adding the code from gist:

<?php
# Nginx don't have PATH_INFO
if (!isset($_SERVER['PATH_INFO'])) {
    $_SERVER['PATH_INFO'] = substr($_SERVER["ORIG_SCRIPT_FILENAME"], strlen($_SERVER["SCRIPT_FILENAME"]));
}

$request = substr($_SERVER['PATH_INFO'], 1);
$file = $request;
$fp = @fopen($file, 'rb');

$size   = filesize($file); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte

header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {

    $c_start = $start;
    $c_end   = $end;

    list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if (strpos($range, ',') !== false) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    if ($range == '-') {
        $c_start = $size - substr($range, 1);
    }else{
        $range  = explode('-', $range);
        $c_start = $range[0];
        $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    $start  = $c_start;
    $end    = $c_end;
    $length = $end - $start + 1;
    fseek($fp, $start);
    header('HTTP/1.1 206 Partial Content');
}

header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);


$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {

    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}

fclose($fp);
exit();
?>

Upvotes: 4

omar hatem
omar hatem

Reputation: 1989

The problem was in the API returning videoes links, it seems like on iOS it needs a header to specify the range of the bytes to be sent. by adding this header the videos worked as expected.

Upvotes: 5

Related Questions