Skip to content

Instantly share code, notes, and snippets.

@Avjeet
Created June 18, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Avjeet/60bfbc80d8b0938d0ed9fa037b00d389 to your computer and use it in GitHub Desktop.
Save Avjeet/60bfbc80d8b0938d0ed9fa037b00d389 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity(), CacheDataSource.EventListener, TransferListener {
private var player: SimpleExoPlayer? = null
companion object {
// About 10 seconds and 1 meg.
// const val VIDEO_URL = "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4"
// About 1 minute and 5.3 megs
const val VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
// The full movie about 355 megs.
// const val VIDEO_URL = "http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4"
// Use to download video other than the one you are viewing. See #3 test of the answer.
// const val VIDEO_URL_LIE = "http://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_480_1_5MG.mp4"
// No changes in code deleted here.
//NOTE: I know I shouldn't use an AsyncTask. It's just a sample...
@SuppressLint("StaticFieldLeak")
fun tryShareCacheFile() {
// file is cached and ready to be used
object : AsyncTask<Void?, Void?, File>() {
override fun doInBackground(vararg params: Void?): File {
val tempFile = FilesPaths.FILE_TO_SHARE.getFile(this@MainActivity, true)
getCachedData(this@MainActivity, cache, VIDEO_URL, tempFile)
return tempFile
}
override fun onPostExecute(result: File) {
super.onPostExecute(result)
val intent = prepareIntentForSharingFile(this@MainActivity, result)
startActivity(intent)
}
}.execute()
}
private var mTotalBytesToRead = 0L
private var mBytesReadFromCache: Long = 0
private var mBytesReadFromNetwork: Long = 0
@WorkerThread
fun getCachedData(
context: Context, myCache: Cache?, url: String, tempfile: File
): Boolean {
var isSuccessful = false
val myUpstreamDataSource = DefaultHttpDataSourceFactory(ExoPlayerEx.getUserAgent(context)).createDataSource()
val dataSource = CacheDataSource(
myCache,
// If the cache doesn't have the whole content, the missing data will be read from upstream
myUpstreamDataSource,
FileDataSource(),
// Set this to null if you don't want the downloaded data from upstream to be written to cache
CacheDataSink(myCache, CacheDataSink.DEFAULT_BUFFER_SIZE.toLong()),
/* flags= */ 0,
/* eventListener= */ this
)
// Listen to the progress of the reads from cache and the network.
dataSource.addTransferListener(this)
var outFile: FileOutputStream? = null
var bytesRead = 0
// Total bytes read is the sum of these two variables.
mTotalBytesToRead = C.LENGTH_UNSET.toLong()
mBytesReadFromCache = 0
mBytesReadFromNetwork = 0
try {
outFile = FileOutputStream(tempfile)
mTotalBytesToRead = dataSource.open(DataSpec(Uri.parse(url)))
// Just read from the data source and write to the file.
val data = ByteArray(1024)
Log.d("getCachedData", "<<<<Starting fetch...")
while (bytesRead != C.RESULT_END_OF_INPUT) {
bytesRead = dataSource.read(data, 0, data.size)
if (bytesRead != C.RESULT_END_OF_INPUT) {
outFile.write(data, 0, bytesRead)
}
}
isSuccessful = true
} catch (e: IOException) {
// error processing
} finally {
dataSource.close()
outFile?.flush()
outFile?.close()
}
return isSuccessful
}
override fun onCachedBytesRead(cacheSizeBytes: Long, cachedBytesRead: Long) {
Log.d("onCachedBytesRead", "<<<<Cache read? Yes, (byte read) $cachedBytesRead (cache size) $cacheSizeBytes")
}
override fun onCacheIgnored(reason: Int) {
Log.d("onCacheIgnored", "<<<<Cache ignored. Reason = $reason")
}
override fun onTransferInitializing(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
Log.d("TransferListener", "<<<<Initializing isNetwork=$isNetwork")
}
override fun onTransferStart(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
Log.d("TransferListener", "<<<<Transfer is starting isNetwork=$isNetwork")
}
override fun onTransferEnd(source: DataSource?, dataSpec: DataSpec?, isNetwork: Boolean) {
reportProgress(0, isNetwork)
Log.d("TransferListener", "<<<<Transfer has ended isNetwork=$isNetwork")
}
override fun onBytesTransferred(
source: DataSource?,
dataSpec: DataSpec?,
isNetwork: Boolean,
bytesTransferred: Int
) {
// Report progress here.
if (isNetwork) {
mBytesReadFromNetwork += bytesTransferred
} else {
mBytesReadFromCache += bytesTransferred
}
reportProgress(bytesTransferred, isNetwork)
}
private fun reportProgress(bytesTransferred: Int, isNetwork: Boolean) {
val percentComplete =
100 * (mBytesReadFromNetwork + mBytesReadFromCache).toFloat() / mTotalBytesToRead
val completed = "%.1f".format(percentComplete)
Log.d(
"TransferListener", "<<<<Bytes transferred: $bytesTransferred isNetwork=$isNetwork" +
" $completed% completed"
)
}
// No changes below here.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment