New: TTS for selected text (by Mat from Google Zurich)

This commit is contained in:
Emmanuel Engelhart 2015-06-13 15:11:08 +02:00
parent 1742efefd4
commit c71ff501e7
4 changed files with 67 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_speak_text"
android:icon="@drawable/action_voice"
android:onClick="onContextMenuClicked"
app:showAsAction="always"
/>
</menu>

View File

@ -43,6 +43,7 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
@ -154,6 +155,40 @@ public class KiwixMobileActivity extends AppCompatActivity
private AnimatedProgressBar mProgressBar;
// Initialized when onActionModeStarted is triggered.
private ActionMode mActionMode = null;
@Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
// Inflate custom menu icon.
getMenuInflater().inflate(R.menu.menu_webview_action, menu);
}
super.onActionModeStarted(mode);
}
@Override
public void onActionModeFinished(ActionMode mode) {
mActionMode = null;
super.onActionModeFinished(mode);
}
public void onContextMenuClicked(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_speak_text:
Log.i(TAG_KIWIX, "Speaking selection.");
tts.readSelection();
break;
default:
Log.e(TAG_KIWIX, "Unexpected context menu click.");
break;
}
if (mActionMode != null) {
mActionMode.finish();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {

View File

@ -84,6 +84,12 @@ public class KiwixTextToSpeech {
});
}
/**
* Reads the currently selected text in the WebView.
*/
public void readSelection() {
webView.loadUrl("javascript:tts.speakAloud(window.getSelection().toString());", null);
}
/**
* Starts speaking the WebView content aloud (or stops it if TTS is speaking now).