0

I am doing a task where i have to load the url inside the webview so that i can show the video,everything is works fine but the thing is i need to pause the video inside the web view so video should not autoplay all the time.Any help Appricatied.

MainActivity

class MainActivity : AppCompatActivity() {
 private var txvResult: TextView? = null
        private lateinit var webView:WebView
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            webView = findViewById(R.id.web_view)
         
            webView.loadUrl("https://www.youtube.com/watch?v=Aeoa5ZsJ02U")
            webView.webViewClient = WebViewClient()
            val webSettings:WebSettings = webView.settings
              webSettings.javaScriptEnabled = true 
    }
}

layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button                     
        android:onClick="getSpeechInput"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
Shivam Sharma
  • 63
  • 1
  • 10
  • [Rather Than using a Web View U can Simply Customise the Youtube Video URL Refer this Link](https://stackoverflow.com/questions/7324759/how-to-display-thumbnail-of-youtube-videos-in-android) – Shivam Sharma May 07 '22 at 18:10

1 Answers1

0

You should use the below line to pause webview video

webView!!.onPause()
webView!!.pauseTimers()
webView!!.reload()

Use this where you want it in your code.

Akash Kumar
  • 121
  • 1
  • 7
  • No, U don't need to pause a video using a web view It will freeze your layout and U can't intract with it.Also pausing the web view is not a relaible solutions for a problem causing ur app get freeze if its dependency based on Webview. – Shivam Sharma May 22 '22 at 16:13