如何在Android的WebView中暂停视频

发布于 2025-01-26 20:31:33 字数 2138 浏览 1 评论 0原文

我正在执行一项任务,必须将URL加载到WebView中,以便我可以显示 视频,一切都很好,但是我需要在网络视图中暂停视频,所以 视频不应始终自动播放。有助于使用。

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 
    }
}

布局

<?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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-02-02 20:31:33

您应该使用以下行暂停网络视频

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

在代码中使用它的位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文