Make your content available offline
Staytuned SDK enables your users to download content on their devices to consume it offline.
This feature is not available on the web platform
Before consuming a content offline a user must download a track. The SDK provides the following functions to download and remove a downloaded track.
Android
iOS
Cordova
class MainActivity : AppCompatActivity() {
private fun downloadTrack() {
val track = STTrack()
STOffline.getInstance()?.add(
applicationContext,
track,
object: STHttpCallback<STTrack> {
override fun onSuccess(data: STTrack) {
println("success")
}
override fun onError(t: Throwable) {
Log.e(LOG_TAG, "Error adding offline content : ${track.key}", t)
}
}
)
}
private fun removeDownload() {
val track = STTrack()
STOffline.getInstance()?.remove(applicationContext, track)
}
}
import UIKit
import StayTunedSDK
class SampleViewController: UIViewController {
private func downloadTrack() {
let track = STTrack(/*..*/)
STOffline.shared?.add(track, completionHandler: { result in
switch result {
case .success:
print("success")
case .failure(let error):
print("error: ", error)
}
})
}
private func removeDownload() {
let track = STTrack(/*..*/)
STOffline.shared?.remove(track)
}
}
// Download
await STOffline.getInstance().add(track);
// Remove a download
await STOffline.getInstance().remove(track);
You can access the downloaded content as follow. Please see the API REFERENCE for more details on models.
Android
iOS
Cordova
class MainActivity : AppCompatActivity() {
private fun getDownloadedContents() {
STOffline.getInstance()?.contents?.observe(this) { it ->
// it is a STContentOfflineItem List
}
}
private fun getDownloadedTracks() {
STOffline.getInstance()?.tracks?.observe(this) { it ->
// it is a STTrackOfflineItem List
}
}
}
import UIKit
import StayTunedSDK
class SampleViewController: UIViewController {
private func getDownloadedContents() {
let offlineContents: [STContentLightOfflineItem] = STOffline.shared?.contetnts
}
private func getDownloadedTracks() {
let offlineTracks: [STTrackOfflineItem] = STOffline.shared?.tracks
}
}
// Get Tracks
const trackObserver = STOffline.getInstance()
.observeTracks((tracks: STTrackOfflineItem[]) => {
// your code
});
// Stop the observer
STOffline.getInstance().removeTrackObserver(trackObserver);
Last modified 1yr ago