title: Serve mp3 stream for android with Laravel
body: <p>Here's my problem: I'm writing a laravel backend which have to serve an mp3 file that had to be reproduced by using the android standard media player.<br><br>
  For the laravel backend I need to use JWT to handle authentication so on every request headers I have to set the "Authorization" field to "<em>Bearer {token}</em>" .<br>The laravel route is "<em>/songs/{id}</em>" and is handled in this way:</p>
  
  <pre><code>public function getSong(Song $song) {
      $file = new File(storage_path()."/songs/".$song-&gt;path.".mp3");
  
      $headers = array();
      $headers['Content-Type'] = 'audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3';
      $headers['Content-Length'] = $file-&gt;getSize();
      $headers['Content-Transfer-Encoding'] = 'binary';
      $headers['Accept-Range'] = 'bytes';
      $headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0';
      $headers['Connection'] = 'Keep-Alive';
      $headers['Content-Disposition'] = 'attachment; filename="'.$song-&gt;path.'.mp3"';
  
      $user = \Auth::user();
      if($user-&gt;activated_at) {
          return Response::download($file, $song-&gt;path, $headers);
      }
      \App::abort(400);
  }
  </code></pre>
  
  <p>On the android side I'm using the MediaPlayer to stream the mp3 file in this way:</p>
  
  <pre><code>media_player = new MediaPlayer();
      try {
          media_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
  
          String token = getSharedPreferences("p_shared", MODE_PRIVATE).getString("token", null);
          Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
          headers.put("Authorization", "Bearer " + token);
  
          media_player.setDataSource(
              getApplicationContext(),
              Uri.parse(ConnectionHelper.SERVER + "/songs/" + song.getId()),
              headers
          );
      } catch (IOException e) {
          finish();
          Toast.makeText(
                  Round.this,
                  "Some error occurred. Retry in some minutes.",
                  Toast.LENGTH_SHORT
          ).show();
      }
      media_player.setOnCompletionListener(this);
      media_player.setOnErrorListener(this);
      media_player.setOnPreparedListener(this);
  </code></pre>
  
  <p>But every time I execute the code I get extra code <strong>-1005</strong> on the error listener that means <strong>ERROR_CONNECTION_LOST</strong>. </p>