4

我正在使用 webview 来呈现 epub,并且能够实现文本到语音的功能,但我无法突出显示文本!

我试过了:

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); 
}
}

有人可以帮帮我吗?以下代码不起作用:

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

0 回答 0