'How to disable AutoPlay video in youtube Player Api in Android in kotlin?
I am unable to disable the auto play video in youtube I tried many ways to disable but none of effort worked. How can I disable the video using youtube Api android?
fun playVideo(videoId: String){
qHandlernew?.removeCallbacksAndMessages(null)
var startPoint =
Uri.parse(videoId)
.getQueryParameter("start")
var endPoint: String? =
Uri.parse(videoId)
.getQueryParameter("end")
val part =
videoId.substring(0, videoId.lastIndexOf('?'))
val youtubeId = part.substring(part.length - 11, part.length)
if (startPoint == null)
player?.loadVideo(youtubeId)
else
player?.loadVideo(
youtubeId,
Integer.parseInt(startPoint) * 1000
)
Log.d("videosCheck", "onInitializationSuccess: $videoId")
if (endPoint != null) {
qHandlernew = Handler()
qHandlernew!!.postDelayed(object : Runnable {
override fun run() {
//For every 1 second, check the current time and endTime
if (player?.currentTimeMillis!! <= Integer.parseInt(
endPoint
) * 1000
) {
qHandlernew!!.postDelayed(this, 1000)
} else {
qHandlernew!!.removeCallbacks(this) //no longer required
player?.pause() //and Pause the video
}
}
}, 1000)
}
}
Solution 1:[1]
Just simply do cueVideo(your video id) instead of loadVideo(your Video id)
fun playVideo(videoId: String){
qHandlernew?.removeCallbacksAndMessages(null)
var startPoint =
Uri.parse(videoId)
.getQueryParameter("start")
var endPoint: String? =
Uri.parse(videoId)
.getQueryParameter("end")
val part =
videoId.substring(0, videoId.lastIndexOf('?'))
val youtubeId = part.substring(part.length - 11, part.length)
if (startPoint == null)
player?.cueVideo(youtubeId)
else
player?.cueVideo(
youtubeId,
Integer.parseInt(startPoint) * 1000
)
Log.d("videosCheck", "onInitializationSuccess: $videoId")
if (endPoint != null) {
qHandlernew = Handler()
qHandlernew!!.postDelayed(object : Runnable {
override fun run() {
//For every 1 second, check the current time and endTime
if (player?.currentTimeMillis!! <= Integer.parseInt(
endPoint
) * 1000
) {
qHandlernew!!.postDelayed(this, 1000)
} else {
qHandlernew!!.removeCallbacks(this) //no longer required
player?.pause() //and Pause the video
}
}
}, 1000)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
