Streaming mp3 file from server in Swift 3

advertisements

I am trying to stream mp3 file from my centOS based server , I have uploaded the file to server and tried opening it in AvPlayer , however it actually downloads it and plays it, when I open the same link in browser it is able to stream the file, it starts playing in 5 seconds around. Is there any solution which can do the same in iOS app using swift ?


In my case, I used php for stream that mean IOS app will play the mp3 file via stream.php:

<?php
set_time_limit(0);
$filePath = $dirPath . "/songname.mp3";
$strContext=stream_context_create(
    array(
        'http'=>array(
        'method'=>'GET',
        'header'=>"Accept-language: en\r\n"
        )
    )
);
$fpOrigin=fopen($filePath, 'rb', false, $strContext);
header('Content-Disposition: inline; filename="songname.mp3"');
header('Pragma: no-cache');
header('Content-type: audio/mpeg');
header('Content-Length: '.filesize($filePath));
while(!feof($fpOrigin)){
  $buffer=fread($fpOrigin, 4096);
  echo $buffer;
  flush();
}
fclose($fpOrigin);

Then I pass the url to swift: http://domain/stream.php