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