【问题标题】:Embed Spotify not working properly at Android's Webview在 Android 的 Webview 中嵌入 Spotify 无法正常工作
【发布时间】:2020-05-06 20:20:29
【问题描述】:

我在我的应用程序内的 webview 中使用了 embed spotify,一切正常。但它突然停止工作。现在,每次我单击播放按钮或单击播放列表中的一首歌时,它都会将我重定向到“spotify:playlist:(playlist code)”。我实际上是在捕捉这种意图,但我希望歌曲在 webview 中播放,而不是像以前那样重定向到 spotify 的应用程序。

我的 WebViewClient:

class MyWebViewClient(private val originUrl: String?) : WebViewClient() {

    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        if (Uri.parse(url).scheme.containsIntent(listOf("market", "whatsapp", "spotify"))){
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse(url)
            val host: Activity = view?.context as Activity
            host.startActivity(intent)

            view.loadUrl(originUrl)
            return true
        }


        return false
    }
}

我实际上是在回收站视图中初始化它:

class WebViewHolder(itemView: View) : ViewHolder(itemView),
    HolderInterface {
    private val webView: WebView = itemView.findViewById(R.id.webview)
    private var link: String? = null

    init {
        webView.webChromeClient = WebChromeClient()
    }

    override fun bind(message: Message) {
        if (link == null){
            link = message.text

            webView.webViewClient = MyWebViewClient(link)

            webView.settings.javaScriptEnabled = true

            webView.setOnTouchListener { view, _ ->
                view.parent.requestDisallowInterceptTouchEvent(true)
                false
            }

            webView.loadUrl(link)
        }
    }
}

用这个布局给它充气:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:paddingLeft="15dp"
    android:paddingStart="15dp"
    android:paddingRight="15dp"
    android:paddingEnd="15dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" >

    <androidx.cardview.widget.CardView
        android:id="@+id/bot_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="1dp"
        android:thicknessRatio="1.9"
        app:cardCornerRadius="30dp">

        <ImageView
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_gravity="center"
            android:src="@drawable/chat_bot_avatar"/>
    </androidx.cardview.widget.CardView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:layout_toEndOf="@id/bot_image"
        android:layout_toRightOf="@id/bot_image">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:clickable="true" />

    </RelativeLayout>

</RelativeLayout>

我正在使用此链接测试此 web 视图:https://open.spotify.com/embed/playlist/27XP00bk55vpUc9KaQoIjz

它曾经在我的回收站视图中工作。从那以后,我没有对我的 webview 文件进行任何更改。

这段代码有问题吗?

[编辑]

我实际上发现大多数手机都不会发生此错误。到目前为止,我是唯一受影响的,不仅在应用程序上,而且在浏览器上(包含 embed spotify 的网站)。有没有人有任何想法?

【问题讨论】:

    标签: android kotlin webview embed spotify


    【解决方案1】:

    Spotify 需要 Webview 的外部权限。

    尝试在您的WebChromeClient 中实现onPermissionRequest()

     override fun onPermissionRequest(request: PermissionRequest?) {
                val resources = request?.resources
                for (i in resources?.indices!!) {
                    if (PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID == resources[i]) {
                        request.grant(resources)
                        return
                    }
                }
                super.onPermissionRequest(request)
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2017-03-16
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多