Additional Features of Caching API - Offline Playback

Introduction

This articles helps to describes additional features for Offline Playback using different properties/methods of THEO Caching API.

The various status of the caching API are as follows:

  • Idle: This means that the task has been created, but not downloading content at the moment.
  • Loading: This means that the task is currently downloading the content.
  • Done: This means that the task has finished downloading all content.
  • Error: This means that the task has encountered an error while downloading or evicting content.
  • Evicted: This means that all data associated with the task has been removed because the task expired or the user invoked the remove method.

Code Example 

How to Pause or Resume a downloading Stream:

//For Android SDK 
private
 void startAndPauseTask(CachingTask task) {

  //Below will move from "Loading" status to "Idle" and pause the downloading 
    task.pause();
//Below will move from "Idle" status to "Loading" and resume the downloading
   
task.start();
}
//For iOS SDK
        
// pausing a caching task
        cachingTask?.pause()

        // resuming a caching task
        cachingTask?.start()

How to check size and progression of downloading Stream:

After creating a caching task, it is possible to check an estimate for the total size of the caching task on disk, and the current progression in bytes (stored).

//For Android SDK
private
 void checkStorageRequirements(CachingTask unstartedTask) {

    // We can get an estimate for the amount of space this task will consume. This will be ready once the manifest is preprocessed when the task is created. Starting is not necessary

    System.out.println(unstartedTask.getBytes());
    unstartedTask.start();
 
    // ... Additional logic here
    // At any time, we can check the current amount of bytes cached by the task

    System.out.println(unstartedTask.getBytesCached());
}
//For iOS SDK
if
 let cachingTask = cachingTask {
            // Listen for state change events
            _ = cachingTask.addEventListener(type: CachingTaskEventTypes.STATE_CHANGE, listener: { event in
                print(cachingTask.status)
            })

            // Listen for progress events
            _ = cachingTask.addEventListener(type: CachingTaskEventTypes.PROGRESS, listener: { event in
                // percentage cached
                print(cachingTask.percentageCached)
                // total bytes cached
                print(cachingTask.bytesCached)
                // the amount of seconds cached
                print(cachingTask.secondsCached)
                // the range that is cached

                for timeRange in cachingTask.cached {
                    print(timeRange.start, timeRange.end)
                }
            })
        }

How to delete a downloaded Stream:

Below example deletes all the downloaded assets, while an individual file can also be deleted as per your user by comparing the source 

//For Android SDK
private
void cleanCache() {
    for (CachingTask task: sharedInstance.getCache().getTasks()) {
        //To delete all the downloaded assets

        task.remove();
    }
}

 

//For iOS SDK
func cleanCache() {

    for cachingTask in THEOplayer.cache.tasks {
        print("Will remove caching task \(cachingTask.source.sources[0].src)")
        //To delete a particular asset
        
if cachingTask.source.sources[0].src == source{
        cachingTask.remove()
          
}
      }
 }