Giter Site home page Giter Site logo

iambaljeet / videopreloading Goto Github PK

View Code? Open in Web Editor NEW
71.0 3.0 23.0 151 KB

Demo for video PreLoading/ PreCaching using ExoPlayer 2.13.3 in Android.

Kotlin 100.00%
android android-development android-application android-app android-studio android-ui retrofit2 retrofit2-rxjava2 retrofit2-coroutines retrofit2-converter

videopreloading's Introduction

videopreloading's People

Contributors

iambaljeet avatar ibaljeet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

videopreloading's Issues

HLS issue

I checked this source code for HLS videos, but In Hls the video cant preload.

not supported in new version of exo player

Hello

thanks for the article. I've tried to implement it and to use it in the upgraded version of exoplayer (2.13) but many of the methods signatures are different and there is no support now for CacheUtil.

What changes should I do to support new version of exo player?

thanks

precaching failed to start

good morning dev

i try to use your library within my app with a litle adaptation but due face to java java.lang.NullPointerException but don't understant because i thing all varaible have been pass well

this is my main activity : to pass VideoUrl and VideoList throught intent to VideoPreLoadingService directly without using fragment

class MainActivity : AppCompatActivity(), HomeScreenCallback, AudioWidget.OnWidgetStateChangedListener {

    private var videoList = arrayListOf<String>();
    private var videoUrl: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)

          videoList.add("http://MY_ip_server_adress:80/live/streamkey/audio.mp4")

        startPreLoadingService(videoList[0])
        finish()
    }

    private fun startPreLoadingService(videoUrl: String) {
        val preloadingServiceIntent = Intent(applicationContext, VideoPreLoadingService::class.java)
        preloadingServiceIntent.putStringArrayListExtra(Constants.VIDEO_LIST, videoList)
        preloadingServiceIntent.putExtra(Constants.VIDEO_URL, videoUrl)
        applicationContext?.startService(preloadingServiceIntent)
    }

    override fun openVideoPlayScreen(videoUrl: String) {
    }

and this is my VideoPreLoadingService to start preloading stream ans play it

    private lateinit var mContext: Context
    private var simpleCache: SimpleCache? = null
    private var cachingJob: Job? = null
    private var videosList: ArrayList<String>? = null

    private var cacheDataSourceFactory: CacheDataSourceFactory? = null
    private var simpleExoPlayer: SimpleExoPlayer? = null

    private var videoUrl: String? = null

    override fun onHandleIntent(intent: Intent?) {
        mContext = applicationContext
        simpleCache = MyApp.simpleCache

        if (intent != null) {
            val extras = intent.extras
            videoUrl = extras?.getString(Constants.VIDEO_URL)
            videosList = extras?.getStringArrayList(Constants.VIDEO_LIST)

            if (!videosList.isNullOrEmpty()) {
                preCacheVideo(videosList)
            }
        }
        initPlayer()
    }

    private fun initPlayer() {
        simpleExoPlayer = mContext.let {
            SimpleExoPlayer.Builder(it).build()
        }

        simpleCache = MyApp.simpleCache

        cacheDataSourceFactory = CacheDataSourceFactory(
                simpleCache,
                DefaultHttpDataSourceFactory(mContext.let { Util.getUserAgent(it, "live") }),
                CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR
        )

        val videoUri = Uri.parse(videoUrl)
        val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(videoUri)

        //playerView.player = simpleExoPlayer
        simpleExoPlayer?.playWhenReady = true
        simpleExoPlayer?.seekTo(0, 0)
        simpleExoPlayer?.repeatMode = Player.REPEAT_MODE_OFF
        simpleExoPlayer?.prepare(mediaSource, true, false)
        simpleExoPlayer?.addListener(this)
    }

    private fun preCacheVideo(videosList: ArrayList<String>?) {
        //var videoUrl: String? = null
        if (!videosList.isNullOrEmpty()) {
            videoUrl = videosList[0]
            videosList.removeAt(0)
        } else {
            stopSelf()
        }
        if (!videoUrl.isNullOrBlank()) {
            val videoUri = Uri.parse(videoUrl)
            val dataSpec = DataSpec(videoUri)
            val defaultCacheKeyFactory = CacheUtil.DEFAULT_CACHE_KEY_FACTORY
            val progressListener = CacheUtil.ProgressListener { requestLength, bytesCached, newBytesCached -> val downloadPercentage: Double = (bytesCached * 100.0 / requestLength) }
            val dataSource: DataSource = DefaultDataSourceFactory(mContext, Util.getUserAgent(this, "live")).createDataSource()

            cachingJob = GlobalScope.launch(Dispatchers.IO) { cacheVideo(dataSpec, defaultCacheKeyFactory, dataSource,progressListener )
                preCacheVideo(videosList)
            }
        }
    }

    private fun cacheVideo(
        dataSpec: DataSpec,
        defaultCacheKeyFactory: CacheKeyFactory?,
        dataSource: DataSource,
        progressListener: CacheUtil.ProgressListener
    ) {
        CacheUtil.cache(
            dataSpec,
            simpleCache,
            defaultCacheKeyFactory,
            dataSource,
            progressListener,
            null
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        cachingJob?.cancel()
    }

don't know what i have do wrong . please need your help

Hello, a question with the result

Hi there! I am developing an application with audio lists and I intend to generate an immediate loading sensation as in the tiktok application. With this project could you achieve that?

Video preload not work properly

video preload not work, it should be like preload the video in background is 5 sec or 10 sec of the video, in this way user data will consume less. you have told over a call me you will add few lines on code for this but still not added i am waiting for this bro please add this as soon as possible. love you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.