title: Playing DRM protected content offline
body: <p>I need to play DRM protected content offline. I two files stored in local storage; a dash file that points to the an mp4 file in the same directory.</p>
  
  <p>The file's key is also stored in a database and retrieved when playback is initiated. </p>
  
  <p>However, after building the DRM session, the following error is thrown:</p>
  
  <pre><code>playerFailed [1.04]
  com.google.android.exoplayer2.ExoPlaybackException
      at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.feedInputBuffer(MediaCodecRenderer.java:691)
      at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:507)
      at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:466)
      at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:300)
      at android.os.Handler.dispatchMessage(Handler.java:98)
      at android.os.Looper.loop(Looper.java:148)
      at android.os.HandlerThread.run(HandlerThread.java:61)
      at com.google.android.exoplayer2.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40)
   Caused by: android.media.MediaCodec$CryptoException: Error decrypting data: requested key has not been loaded
      at android.media.MediaCodec.native_queueSecureInputBuffer(Native Method)
      at android.media.MediaCodec.queueSecureInputBuffer(MediaCodec.java:2292)
      at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.feedInputBuffer(MediaCodecRenderer.java:682)
      at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:507) 
      at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:466) 
      at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:300) 
      at android.os.Handler.dispatchMessage(Handler.java:98) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.os.HandlerThread.run(HandlerThread.java:61) 
      at com.google.android.exoplayer2.util.PriorityHandlerThread.run(PriorityHandlerThread.java:40) 
  </code></pre>
  
  <p>I use this block to check whether a given track has a key or not before requesting for a new one. </p>
  
  <pre><code>private DrmSessionManager&lt;FrameworkMediaCrypto&gt; buildDrmSessionManager(UUID uuid, String licenseURL,
                                                                             Map&lt;String, String&gt; keyRequestProperties) throws UnsupportedDrmException {
          HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseURL, buildHttpDataSourceFactory(false), keyRequestProperties);
  
          defaultDrmSessionManager = new DefaultDrmSessionManager&lt;&gt;(uuid, FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, eventLogger);
  
          if (track != null) {
              DrmKey key = realm.where(DrmKey.class).equalTo("trackId", track.getId()).findFirst();
  
              Log.e("TrackId", track.getId());
  
              if (key != null) {
                  byte[] offlineKeySetId = Base64.decode(key.getRequestKey(), Base64.DEFAULT);
                  defaultDrmSessionManager.setMode(DefaultDrmSessionManager.MODE_QUERY, offlineKeySetId);
                  Log.e("Key", key.getRequestKey());
  
                  Log.e(MusicService.class.getSimpleName(), "OfflineKeySet already defined, using the existing one : " + key.getRequestKey());
              } else {
                  defaultDrmSessionManager.setMode(DefaultDrmSessionManager.MODE_DOWNLOAD, null);
                  Log.e(MusicService.class.getSimpleName(), "Offline key not defined, retrieving a new licence");
              }
          }
  
          return defaultDrmSessionManager;
      }
  </code></pre>
  
  <p>For all the offline files I try to play, the key is always available. Why then is it that exoplayer fails to play any of the files?</p>