Skip to content

Instantly share code, notes, and snippets.

View prateek54's full-sized avatar
👨‍💻

Prateek Batra prateek54

👨‍💻
View GitHub Profile
@prateek54
prateek54 / build.gradle
Created October 21, 2023 22:14
Dependencies to add Media 3 Transformer
implementation "androidx.media3:media3-transformer:1.1.1"
implementation "androidx.media3:media3-effect:1.1.1"
implementation "androidx.media3:media3-common:1.1.1"
@prateek54
prateek54 / TransformerActivity.kt
Created October 21, 2023 21:00
Function to Convert Video to Grayscale using Media Transaformer
private fun convertToGrayScale() {
val effect = arrayListOf<Effect>()
effect.add(RgbFilter.createGrayscaleFilter())
val transformer = with(Transformer.Builder(this)) {
addListener(object : Transformer.Listener {
override fun onCompleted(composition: Composition, exportResult: ExportResult) {
//Play the video from the path you specified below
}
override fun onError(
@prateek54
prateek54 / TransforVideoMethod.kt
Last active October 24, 2023 15:18
Method to Transform the video
val effect = arrayListOf<Effect>()
effect.add(RgbFilter.createGrayscaleFilter())
effect.add(MatrixTransformationFactory.createZoomInTransition())
effect.add(ScaleAndRotateTransformation.Builder().setRotationDegrees(90F).build())
val transformer = with(Transformer.Builder(this)) {
addListener(object : Transformer.Listener {
override fun onCompleted(composition: Composition, exportResult: ExportResult) {
//Play the video from the path you specified below
}
@prateek54
prateek54 / OfflinePlayerActivity.kt
Created March 24, 2023 20:39
Init Block to play offline downloaded video
ExoPlayer player = ExoPlayer.Builder(this).build()
player?.playWhenReady = true
val mediaSource = downloadTracker?.getDownloadRequest(Uri.parse(VIDEO_URL))!!.let {
DownloadHelper.createMediaSource(
it,
DemoUtil.getDataSourceFactory(this)
)
}
player?.setMediaSource(mediaSource)
player?.prepare()
@prateek54
prateek54 / DownloadTracker.java
Last active March 24, 2023 20:30
Block of code to start/stop offline download
public void toggleDownload(
FragmentManager fragmentManager,
MediaItem mediaItem,
RenderersFactory renderersFactory
) {
Download download = downloads.get(checkNotNull(mediaItem.localConfiguration).uri);
if (download != null && download.state != Download.STATE_FAILED) {
DownloadService.sendRemoveDownload(
/* context= */ context,
/* DownloadService= */ OfflineVideoDownloadService.class,
@prateek54
prateek54 / CustomContract.kt
Created May 21, 2022 18:56
Custom Result Contract Kotlin Class
class CustomContract : ActivityResultContract<String, String?>() {
const val DATA = "data"
const val INPUT_DATA = "input_data"
override fun createIntent(context: Context, input: String?): Intent {
val intent = Intent(context, DummyResultActivity::class.java)
intent.putExtra(INPUT_DATA, input)
return intent
}
@prateek54
prateek54 / ExoPlayerActivity.kt
Created January 20, 2022 21:30
Init for ExoPlayer with cache data source
val mediaItem = MediaItem.fromUri(VIDEO_URL)
val mediaSource =
HlsMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaItem)
player.setMediaSource(mediaSource)
@prateek54
prateek54 / ExoPlayerActivity.kt
Last active January 20, 2022 20:57
Init code for Cache Data Source Factory
val cacheSink = CacheDataSink.Factory()
.setCache(downloadCache)
val upstreamFactory = DefaultDataSource.Factory(this, DefaultHttpDataSource.Factory())
val downStreamFactory = FileDataSource.Factory()
val cacheDataSourceFactory =
CacheDataSource.Factory()
.setCache(downloadCache)
.setCacheWriteDataSinkFactory(cacheSink)
.setCacheReadDataSourceFactory(downStreamFactory)
.setUpstreamDataSourceFactory(upstreamFactory)
@prateek54
prateek54 / ExoPlayerActivity.kt
Last active March 28, 2023 10:23
Init method for Download Cache
val DOWNLOAD_CONTENT_DIRECTORY = "downloads"
val downloadContentDirectory =
File(getExternalFilesDir(null),DOWNLOAD_CONTENT_DIRECTORY)
var downloadCache =
SimpleCache(downloadContentDirectory, NoOpCacheEvictor(), StandaloneDatabaseProvider(this))
@prateek54
prateek54 / ExoPlayerActivity.kt
Created January 18, 2022 21:10
Initialisation Code for you player setup with DRM
val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
val drmConfig = MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID)
.setLicenseUri(LICENSE_URL)
val mediaItem = MediaItem.Builder()
.setUri(VIDEO_URL)
.setDrmConfiguration(drmConfig.build())
val mediaSource = DashMediaSource.Factory(defaultHttpDataSourceFactory)
.createMediaSource(mediaItem.build())
player.setMediaSource(mediaSource)