title: Android ExoPlayer - downloading video (non DASH / HLS) and streaming at the same time
body: <p>I would like to download a video that is streaming in ExoPlayer.</p>
  
  <p>As an aside and even before using ExoPlayer I downloaded a file from an input stream provided by <code>HttpURLConnection</code> and played the file from local storage. This is ok, however it does not solve my problem of simultaneous streaming and caching.</p>
  
  <p>ExoPlayer also provides a caching system and these seem to work only for DASH or HLS stream types. I am using none of these and want to cache mp4 with the <code>ExtractorRendererBuilder</code>. (This topic is covered quite extensively here: <a href="https://github.com/google/ExoPlayer/issues/420" rel="noreferrer">https://github.com/google/ExoPlayer/issues/420</a>).</p>
  
  <p><code>DefaultHttpDataSource</code> does have an api that exposes <code>HttpURLConnection</code> but I am not sure I am reusing the stream. Here is the code from the samples provided in ExoPlayer. </p>
  
  <pre><code>    @Override
      public void buildRenderers(DemoPlayer player) {
          Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
          Handler mainHandler = player.getMainHandler();
  
          // Build the video and audio renderers.
          DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(mainHandler, null);
          DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter,userAgent);
          ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri,dataSource,allocator,
                  BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE, mainHandler, player, 0);
          MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(context,
                  sampleSource, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000,
                  mainHandler, player, 50);
          MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,
                  MediaCodecSelector.DEFAULT, null, true, mainHandler, player,
                  AudioCapabilities.getCapabilities(context), AudioManager.STREAM_MUSIC);
          TrackRenderer textRenderer = new TextTrackRenderer(sampleSource, player,
                  mainHandler.getLooper());
  
          // Invoke the callback.
          TrackRenderer[] renderers = new TrackRenderer[DemoPlayer.RENDERER_COUNT];
          renderers[DemoPlayer.TYPE_VIDEO] = videoRenderer;
          renderers[DemoPlayer.TYPE_AUDIO] = audioRenderer;
          renderers[DemoPlayer.TYPE_TEXT] = textRenderer;
          player.onRenderers(renderers, bandwidthMeter);
      }
  </code></pre>
  
  <p>What I have done now is extend <code>DefaultHttpDataSource</code>and use HttpURLConnection to get an InputStream, write to a file in <code>getCacheDir()</code>. Here is the class that extends <code>DefaultHttpDataSource</code>:</p>
  
  <pre><code>public class CachedHttpDataSource extends DefaultHttpDataSource {
      public CachedHttpDataSource(String userAgent, Predicate&lt;String&gt; contentTypePredicate) {
          super(userAgent, contentTypePredicate);
      }
  
      public CachedHttpDataSource(String userAgent, Predicate&lt;String&gt; contentTypePredicate, TransferListener listener) {
          super(userAgent, contentTypePredicate, listener);
      }
  
      public CachedHttpDataSource(String userAgent, Predicate&lt;String&gt; contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis) {
          super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis);
      }
  
      public CachedHttpDataSource(String userAgent, Predicate&lt;String&gt; contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects) {
          super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis, allowCrossProtocolRedirects);
      }
  
      public HttpURLConnection getURLConnection(){
          HttpURLConnection connection = getConnection();
  
          return getConnection();
      }
  }
  </code></pre>
  
  <p>I can now get an InputStream via getURLConnection() to save video into a file but I am not really happy using an InputStream again to cache the video. Is there any other API/ or method that gives access to a byte array that I can write to a file while streaming takes place ? </p>
  
  <p>My other stackoverflow searches have not given a solution yet:</p>
  
  <p><a href="https://stackoverflow.com/questions/28700391/using-cache-in-exoplayer">Using cache in ExoPlayer</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26095261/exoplayer-cache">ExoPlayer cache</a></p>
  
  <p>Thanks for your time and patience.</p>