在Android webview中加载页面时,ResponsiveVoice不起作用

时间:2017-05-20 19:52:18

标签: javascript android webview responsivevoice

单击按钮时效果很好:



<!DOCTYPE html>
<html>
<head>


    <script src="https://code.responsivevoice.org/responsivevoice.js"></script>

    </head >

<body>

<button onclick="responsiveVoice.speak('Welcome to the Responsive Voice website');">Click</button>

</body>
</html>
&#13;
&#13;
&#13;

但是当我想在网页浏览中加载网页时播放语音​​时,该功能无效:

<!DOCTYPE html>
<html>
<head>

    <script src="https://code.responsivevoice.org/responsivevoice.js"></script>

    </head >

<body>

<script>

   setTimeout(responsiveVoice.speak("Welcome to the Responsive Voice  website"),500);

</script>
</body>
</html>

Android代码:

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.myWebview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/index.html");


    }

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abdallah.test">

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission  android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

当我点击按钮时,这是系统的日志:

I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(63)] "RV: speechSynthesis present but no system voices found", source: https://code.responsivevoice.org/responsivevoice.js (63)
I/chromium: [INFO:CONSOLE(64)] "RV: Enabling fallback mode", source: https://code.responsivevoice.org/responsivevoice.js (64)
D/MediaResourceGetter: ethernet/wifi connection detected
D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= SonyD5803 Build/23.4.A.1.232 stagefright/1.2 (Linux;Android 5.1.1)
D/MediaHTTPConnection: proxy null port 0
D/MediaResourceGetter: resource doesn't have video
D/MediaResourceGetter: extracted valid metadata: MediaMetadata[durationInMilliseconds=0, width=0, height=0, success=true]
E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
D/MediaPlayer: Couldn't open file on client side, trying server side
D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= SonyD5803 Build/23.4.A.1.232 stagefright/1.2 (Linux;Android 5.1.1)
D/MediaHTTPConnection: filterOutInternalHeaders: key=allow-cross-domain-redirect, val= false
D/MediaHTTPConnection: filterOutInternalHeaders: key=x-wap-profile, val= http://uaprof.sonymobile.com/D5803R2321.xml
D/MediaHTTPConnection: proxy null port 0
E/MediaPlayer: Should have subtitle controller already set
D/MediaPlayer: getMetadata

这是我想在加载页面时播放语音​​的时间:

I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(62)] "Voice support NOT ready", source: https://code.responsivevoice.org/responsivevoice.js (62)
I/chromium: [INFO:CONSOLE(63)] "RV: speechSynthesis present but no system voices found", source: https://code.responsivevoice.org/responsivevoice.js (63)
I/chromium: [INFO:CONSOLE(64)] "RV: Enabling fallback mode", source: https://code.responsivevoice.org/responsivevoice.js (64)
D/MediaResourceGetter: ethernet/wifi connection detected
D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= SonyD5803 Build/23.4.A.1.232 stagefright/1.2 (Linux;Android 5.1.1)
D/MediaHTTPConnection: proxy null port 0
D/MediaResourceGetter: resource doesn't have video
D/MediaResourceGetter: extracted valid metadata: MediaMetadata[durationInMilliseconds=0, width=0, height=0, success=true]

1 个答案:

答案 0 :(得分:0)

ResponsiveVoice说:

在iOS上,需要通过用户操作(即点击)触发某些事件,例如语音合成。例如,这导致ResponsiveVoice不会在页面加载时调用调用。

使用语音合成,用户触发的操作只需要完成一次。第一个之后的进一步直接调用将按预期工作。

因此,建议的最佳做法是为用户添加“开始”按钮以供用户单击,并将其用作对responsiveVoice.speak()的初始化调用。如果需要,该调用可以有一个空格作为文本,因此对用户没有影响。

我认为android是一样的,但我找到了解决问题的方法:

首先,我将加载包含文本到语音的javascript函数的html页面。 然后只加载函数本身,如下面的代码:

Dictionary<string, string> settings = new Dictionary<string, string>
{
    { "unsat-core", "false" },    // enable generation of unsat cores
    { "model", "false" },         // enable model generation
    { "proof", "false" },         // enable proof generation
    { "timeout", "60000" }        // 60000=1min
};
Context ctx = new Context(settings);

Solver solver = ctx.MkSolver(ctx.MkTactic("qfbv"));
BitVecExpr rax0 = ctx.MkBVConst("RAX!0", 64);
BitVecExpr rax1 = ctx.MkBVConst("RAX!1", 64);
BitVecExpr rax2 = ctx.MkBVConst("RAX!2", 64);

BitVecExpr rbx0 = ctx.MkBVConst("RBX!0", 64);
BitVecExpr rbx1 = ctx.MkBVConst("RBX!1", 64);
BitVecExpr rbx2 = ctx.MkBVConst("RBX!2", 64);

BitVecExpr rcx0 = ctx.MkBVConst("RCX!0", 64);
BitVecExpr rcx1 = ctx.MkBVConst("RCX!1", 64);
BitVecExpr rcx2 = ctx.MkBVConst("RCX!2", 64);

ArrayExpr mem0 = ctx.MkArrayConst("MEM!0", ctx.MkBitVecSort(64), ctx.MkBitVecSort(8));
ArrayExpr mem1 = ctx.MkArrayConst("MEM!1", ctx.MkBitVecSort(64), ctx.MkBitVecSort(8));
ArrayExpr mem2 = ctx.MkArrayConst("MEM!2", ctx.MkBitVecSort(64), ctx.MkBitVecSort(8));

solver.Assert(ctx.MkEq(rax1, rax0));
solver.Assert(ctx.MkEq(rbx1, rbx0));
solver.Assert(ctx.MkEq(rcx1, rcx0));
ArrayExpr memX0 = ctx.MkStore(mem0, ctx.MkBVAdd(ctx.MkBV(0, 64), rax0), ctx.MkExtract((1 * 8) - 1, 0 * 8, rbx0));
ArrayExpr memX1 = ctx.MkStore(memX0, ctx.MkBVAdd(ctx.MkBV(1, 64), rax0), ctx.MkExtract((2 * 8) - 1, 1 * 8, rbx0));
ArrayExpr memX2 = ctx.MkStore(memX1, ctx.MkBVAdd(ctx.MkBV(2, 64), rax0), ctx.MkExtract((3 * 8) - 1, 2 * 8, rbx0));
ArrayExpr memX3 = ctx.MkStore(memX2, ctx.MkBVAdd(ctx.MkBV(3, 64), rax0), ctx.MkExtract((4 * 8) - 1, 3 * 8, rbx0));
ArrayExpr memX4 = ctx.MkStore(memX3, ctx.MkBVAdd(ctx.MkBV(4, 64), rax0), ctx.MkExtract((5 * 8) - 1, 4 * 8, rbx0));
ArrayExpr memX5 = ctx.MkStore(memX4, ctx.MkBVAdd(ctx.MkBV(5, 64), rax0), ctx.MkExtract((6 * 8) - 1, 5 * 8, rbx0));
ArrayExpr memX6 = ctx.MkStore(memX5, ctx.MkBVAdd(ctx.MkBV(6, 64), rax0), ctx.MkExtract((7 * 8) - 1, 6 * 8, rbx0));
memX7 = ctx.MkStore(memX6, ctx.MkBVAdd(ctx.MkBV(7, 64), rax0), ctx.MkExtract((8 * 8) - 1, 7 * 8, rbx0));
solver.Assert(ctx.MkEq(mem1, memX7).Simplify() as BoolExpr);

solver.Assert(ctx.MkEq(rax2, rax1));
solver.Assert(ctx.MkEq(rbx2, rbx1));
BitVecExpr y0 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(0, 64), rax1)) as BitVecExpr;
BitVecExpr y1 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(1, 64), rax1)) as BitVecExpr;
BitVecExpr y2 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(2, 64), rax1)) as BitVecExpr;
BitVecExpr y3 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(3, 64), rax1)) as BitVecExpr;
BitVecExpr y4 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(4, 64), rax1)) as BitVecExpr;
BitVecExpr y5 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(5, 64), rax1)) as BitVecExpr;
BitVecExpr y6 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(6, 64), rax1)) as BitVecExpr;
BitVecExpr y7 = ctx.MkSelect(mem1, ctx.MkBVAdd(ctx.MkBV(7, 64), rax1)) as BitVecExpr;
BitVecExpr y = ctx.MkConcat(y7, ctx.MkConcat(y6, ctx.MkConcat(y5, ctx.MkConcat(y4, ctx.MkConcat(y3, ctx.MkConcat(y2, ctx.MkConcat(y1, y0)))))));
solver.Assert(ctx.MkEq(rcx2, y).Simplify() as BoolExpr);
solver.Assert(ctx.MkEq(mem2, mem1));

Status status_Neg = solver.Check(ctx.MkNot(ctx.MkEq(rbx2, rcx2)));
Console.WriteLine("Status Neg = "+status_Neg); // Go on holiday...