I am unable to highlight text in web view of speaking text in android

I am using webview for epub rendering and I can implement text to speech functionality, but I cannot highlight text!

I tried:

public class Epub extends Activity implements OnInitListener, OnUtteranceCompletedListener {
    ProgressDialog pDialog;
    WebView webview;
    String line, line1 = "", finalstr = "";
    int i = 0;
    Book book;
    String linez;
    String abspath="file://android_asset/Images/";
    private TextToSpeech mTts;
    String htmlTextStr;
    private int MY_DATA_CHECK_CODE = 0;
    private HashMap<String, String> params = new HashMap<String, String>();
    private int uttCount = 0;
    StringTokenizer st;
    private int lastUtterance = -1;
    ArrayList <String> words = new ArrayList<String> ();   
    ArrayList <String> Swords = new ArrayList<String> ();  
    String utteranceId;
    HashMap<String, String> lastSpokenWord = new HashMap<String, String>();
    String s;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Load().execute();
    mTts = new TextToSpeech(this, this);
    Button mBtnSpeak = (Button) findViewById(R.id.btn);
    webview = (WebView) findViewById(R.id.tv);
    mBtnSpeak.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

              doSpeak(htmlTextStr);
           }
       });
    }
class Load extends AsyncTask<String, String, String> 
{

    @Override
   protected void onPreExecute() 
    {
           super.onPreExecute();
           pDialog = new ProgressDialog(Epub.this);
           pDialog.setMessage("Loading Epub...");
           pDialog.setIndeterminate(false);
           pDialog.setCancelable(false);
           pDialog.show();
    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

            AssetManager assetManager = getAssets();
            try {
                Intent j = getIntent();
                final String pos= j.getExtras().getString("product");
                InputStream epubInputStream = assetManager.open(pos+".epub");
                book = (new EpubReader()).readEpub(epubInputStream);
                //coverImage =BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
               // Log.i("epublib", "Coverimage is " + coverImage.getWidth() +  " by " + coverImage.getHeight() + " pixels");

               // DownloadResource("file:///android_asset/");
            } catch (IOException e) {
                Log.e("epublib", e.getMessage());
            }

        Spine spine = book.getSpine(); 
        List<SpineReference> spineList = spine.getSpineReferences() ;
        int count = spineList.size();

        //tv.setText(Integer.toString(count));
        StringBuilder string = new StringBuilder();
        for (int i = 0; count > i; i++) {
            Resource res = spine.getResource(i);

            try {
                InputStream is = res.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                try {
                    while ((line = reader.readLine()) != null) {
                        linez =   string.append(line + "\n").toString();
                        System.err.println("res media"+res.getMediaType());
                        htmlTextStr = Html.fromHtml(linez).toString();
                        Log.e("Html content.",htmlTextStr);

                    }

                } catch (IOException e) {e.printStackTrace();}

                //do something with stream
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

                webview.getSettings().setAllowFileAccess(true);

                //System.err.println("qaz"+hr);
                webview.getSettings().setBuiltInZoomControls(true);
                webview.getSettings().setJavaScriptEnabled(true);
                webview.loadDataWithBaseURL("file:///android_asset/", linez, "application/xhtml+xml", "UTF-8", null);


        return null; 
    }
      protected void onPostExecute(String file_url) 
      {
              pDialog.dismiss();


      }

}

public void doSpeak(String htmlTextStr) {
    st = new StringTokenizer(htmlTextStr,".");
    while (st.hasMoreTokens()) {
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,String.valueOf(uttCount++));
        mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);
        words.add(htmlTextStr);

    }
 String arr[] = htmlTextStr.split(",");
 for(int i = 0; i < arr.length; i++){
     System.out.println("arr["+i+"] = " + arr[i].trim());
     s=arr[i].trim();
     Swords.add(s);
     runOnUiThread(new Runnable() {
     public void run() {
            webview.findAll(s);
             System.err.println(" b  - - >"+s);
             webview.setSelected(true);
             webview.findNext(true);   
    }
});
 }
  }

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR
    if (status == TextToSpeech.SUCCESS) {
           mTts.setOnUtteranceCompletedListener(this);

        int result = mTts.setLanguage(Locale.UK);
        if (result == TextToSpeech.LANG_MISSING_DATA ||
            result == TextToSpeech.LANG_NOT_SUPPORTED) {
           // Lanuage data is missing or the language is not supported.
            Log.e("404","Language is not available.");
        }
    } else {
        // Initialization failed.
        Log.e("404", "Could not initialize TextToSpeech.");
        // May be its not installed so we prompt it to be installed
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        installIntent.setAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        startActivity(installIntent); 
    }
}
@Override
  public void onPause()
  {
    super.onPause();
    if( mTts != null)
      mTts.stop();
  }
@Override
public void onDestroy() {
    if (mTts != null) {
        mTts.stop();
        mTts.shutdown();
    }
    mTts.stop();
    super.onDestroy();
}    
@Override
public void onUtteranceCompleted(String utteranceId) {
    // TODO Auto-generated method stub

     Log.i("xsw",utteranceId); 
     lastUtterance = Integer.parseInt(utteranceId);

       // createThread(s); 
}
}

      

Can anyone help me? The code below doesn't work:

  webview.findall("string");
  webview.setSelected(true);
  webview.findNext(true);

      

+4


source to share


1 answer


I have not been able to find an exact solution for this. Coincidentally, I'm also working on rendering an epub file.



The approach I had in mind was to split the statements into multiple lines, and each line should have a separate tag. But I haven't been able to implement this, but it seems like a good approach. Please share your solution if you find it.

0


source







All Articles