为什么我听不到音频流URL的声音?

时间:2019-05-15 19:25:50

标签: android audio audio-streaming android-studio-3.4

更新05/20/2019 :使用权限和-功能已添加到清单;同样的结果。下面对文件脚本进行了更新。

我有一个加载网址的webView活动。该网址的网站上有一个播放按钮,单击该按钮即可打开音频流。虽然该网址将加载到webView中,并且用户可以单击“播放”按钮以打开音频流,但是我听不到任何声音,就像我将该网址加载到默认的手机浏览器(如野生动物园)中那样。

我以为webView方法可以用,但不能让我听到声音。

我认为这可能与清单中的权限有关,所以在这里...

package com.app.sega;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class wvop extends AppCompatActivity {
    private WebView webview_v;
        public class WebInterface_v {
            Context vContext;

            WebInterface_v (Context c) {
                vContext = c;
            }

            @JavascriptInterface
            public void playSound(String toast) {
                Toast.makeText(vContext, toast, Toast.LENGTH_SHORT).show();
            }

            @JavascriptInterface
            public void pauseSound(String toast) {
                Toast.makeText(vContext, toast, Toast.LENGTH_SHORT).show();
            }
        }
    private class WebViewClient_v extends WebViewClient {

        public boolean shouldOverrideURLLoading (WebView view, String url) {
            if (Uri.parse(url).getHost().equals("https://us7.maindigitalstream.com/2782/")) {
                return false;
            }else {
                Intent intent_wvop = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent_wvop);
                return true;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wvop);

        webview_v = (WebView) findViewById(R.id.webview_wvop);
        webview_v.setInitialScale(1);
        webview_v.setWebViewClient(new WebViewClient_v());
        WebSettings webSettings_v = webview_v.getSettings();
            webSettings_v.setJavaScriptEnabled(true);
            webSettings_v.setAllowContentAccess(true);
            webSettings_v.setDomStorageEnabled(true);
        webview_v.getSettings().getJavaScriptEnabled();
        webview_v.getSettings().setJavaScriptEnabled(true);
        webview_v.addJavascriptInterface(new WebInterface_v (this), "Android");
        webview_v.getSettings().getLoadWithOverviewMode();
        webview_v.getSettings().setUseWideViewPort(true);
        webview_v.getSettings().setAllowFileAccess(true);
        webview_v.getSettings().setAllowFileAccessFromFileURLs(true);
        webview_v.getSettings().setAllowContentAccess(true);
        webview_v.setScrollbarFadingEnabled(false);
        webview_v.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webview_v.getSettings().getBuiltInZoomControls();
        webview_v.getSettings().getUseWideViewPort();
        webview_v.getSettings().setDomStorageEnabled(true);
        webview_v.getSettings().setUserAgentString("silly_that_i_have_to_do_this");
        webview_v.loadUrl("https://us7.maindigitalstream.com/2782/");
        int SDK_INT = android.os.Build.VERSION.SDK_INT;
            if (SDK_INT > 16) {
            webview_v.getSettings().setMediaPlaybackRequiresUserGesture(false);
        }
    }


}


以防万一是java;这就是...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView android:id="@+id/webview_wvop"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我认为这与布局无关,但以防万一,这里是...

int SDK_INT = android.os.Build.VERSION.SDK_INT;
            if (SDK_INT > 16) {
            webview_v.getSettings().setMediaPlaybackRequiresUserGesture(false);

该页面可以很好地加载,并且从所有视觉角度来看,它都可以正常运行,但是没有声音。我已经检查过手机以确保它打开了。我添加了

WebSettings webSettings_v = webview_v.getSettings();
            webSettings_v.setJavaScriptEnabled(true);
            webSettings_v.setAllowContentAccess(true);
            webSettings_v.setDomStorageEnabled(true);
根据此http://rat32.com/rat32/2018/12/20/how-to-solve-notallowederror-play-can-only-be-initiated-by-a-user-gesture-audio-problem-in-javascript-android-webview/的建议

到Java,但是我得到了相同的结果。在这篇文章的顶部,我已经将其添加到原始Java中。

我去搜寻更多信息,发现:Online audio streaming websites are not working properly on WebView

按照建议,我在上面的java中添加了以下内容

public class WebInterface_v {
        Context vContext;

        WebInterface_v (Context c) {
            vContext = c;
        }

        @JavascriptInterface
        public void playSound(String toast) {
            Toast.makeText(vContext, toast, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void pauseSound(String toast) {
            Toast.makeText(vContext, toast, Toast.LENGTH_SHORT).show();
        }
    }

但是我仍然得到相同的结果。返回精彩的www。

只需在我的java顶部添加另一部分。这是我添加的内容

Hello, :smile::hearth: world!

尽管仍然无法正常工作...如果有人提出建议,仍在寻找解决方案。

0 个答案:

没有答案
相关问题