{"id":1273,"date":"2023-03-18T10:01:44","date_gmt":"2023-03-18T02:01:44","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1273"},"modified":"2023-04-29T09:23:33","modified_gmt":"2023-04-29T01:23:33","slug":"implementation-of-android-automatic-reading-tts","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/implementation-of-android-automatic-reading-tts\/","title":{"rendered":"Android\u81ea\u52a8\u6717\u8bfb\uff08TTS\uff09\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>Android\u63d0\u4f9b\u4e86\u81ea\u52a8\u6717\u8bfb\u652f\u6301\u3002\u53ef\u4ee5\u5bf9\u6307\u5b9a\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u6717\u8bfb\uff0c\u4ece\u800c\u53d1\u751f\u58f0\u97f3\uff1b\u8fd8\u5141\u8bb8\u628a\u6587\u672c\u5bf9\u5e94\u7684\u97f3\u9891\u5f55\u5236\u6210\u97f3\u9891\u6587\u4ef6\uff0c\u65b9\u4fbf\u4ee5\u540e\u64ad\u653e\u3002Android\u7684\u81ea\u52a8\u6717\u8bfb\u4e3b\u8981\u901a\u8fc7TextToSpeech\u6765\u5b8c\u6210\uff0c\u6784\u9020\u5668\u5982\uff1a<code>TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);<\/code>\u5f53\u521b\u5efaTextToSpeech\u5bf9\u8c61\u65f6\uff0c\u5fc5\u987b\u5148\u63d0\u4f9b\u4e00\u4e2aOnInitListener\u76d1\u542c\u5668\u8d1f\u8d23\u76d1\u542cTextToSpeech\u7684\u521d\u59cb\u5316\u7ed3\u679c\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4f7f\u7528TextToSpeech\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u521b\u5efaTextToSpeech\u5bf9\u8c61\uff0c\u521b\u5efa\u65f6\u4f20\u5165OnInitListener\u76d1\u542c\u5668\u76d1\u542c\u793a\u8303\u521b\u5efa\u6210\u529f<\/li>\n<li>\u8bbe\u7f6eTextToSpeech\u6240\u4f7f\u7528\u8bed\u8a00\u56fd\u5bb6\u9009\u9879\uff0c\u901a\u8fc7\u8fd4\u56de\u503c\u5224\u65adTTS\u662f\u5426\u652f\u6301\u8be5\u8bed\u8a00\u3001\u56fd\u5bb6\u9009\u9879<\/li>\n<li>\u8c03\u7528speak()\u6216synthesizeToFile\u65b9\u6cd5<\/li>\n<li>\u5173\u95edTTS\uff0c\u56de\u6536\u8d44\u6e90<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MainActivity extends AppCompatActivity {\n\n    private EditText input;\n    private Button speech, record;\n\n    private TextToSpeech textToSpeech;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        DexterPermissionHelper.requestPermissions(this, new DexterPermissionHelper.PermissionsCheckListener() {\n            @Override\n            public void onPermissionsGranted(int permissionType) {\n\n            }\n\n            @Override\n            public void onPermissionsDenied(int permissionType) {\n\n            }\n        });\n\n        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {\n            @Override\n            public void onInit(int status) {\n                if (status == textToSpeech.SUCCESS) {\n                    \/\/int result = textToSpeech.setLanguage(new Locale(&quot;th&quot;, &quot;TH&quot;));\n                    int result = textToSpeech.setLanguage(Locale.CHINA);\n                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE\n                            &amp;&amp; result != TextToSpeech.LANG_AVAILABLE){\n                        Toast.makeText(MainActivity.this, &quot;TTS\u6682\u65f6\u4e0d\u652f\u6301\u8fd9\u79cd\u8bed\u97f3\u7684\u6717\u8bfb\uff01&quot;,\n                                Toast.LENGTH_SHORT).show();\n                    }\n                }\n            }\n        });\n\n        input = (EditText) findViewById(R.id.input_text);\n        speech = (Button) findViewById(R.id.speech);\n        record = (Button) findViewById(R.id.record);\n\n        speech.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                textToSpeech.speak(input.getText().toString(),\n                        TextToSpeech.QUEUE_ADD, null);\n            }\n        });\n\n        record.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View view) {\n                String inputText = input.getText().toString();\n                HashMap&lt;String, String&gt; myHashRender = new HashMap&lt;&gt;();\n                myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);\n                textToSpeech.synthesizeToFile(inputText, myHashRender,\n                        &quot;\/sdcard\/ttsdemo\/record_audio\/sound.wav&quot;);\n                Toast.makeText(MainActivity.this, &quot;\u58f0\u97f3\u8bb0\u5f55\u6210\u529f&quot;, Toast.LENGTH_SHORT).show();\n            }\n        });\n    }\n\n    @Override\n    protected void onDestroy() {\n        if (textToSpeech != null)\n            textToSpeech.shutdown();\n        super.onDestroy();\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;LinearLayout\n    xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;\n    android:orientation=&quot;vertical&quot;&gt;\n\n    &lt;EditText\n        android:id=&quot;@+id\/input_text&quot;\n        android:layout_marginTop=&quot;20dp&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        android:text=&quot;\u5927\u5bb6\u597d&quot;\n        \/&gt;\n\n    &lt;LinearLayout\n        android:layout_marginTop=&quot;10dp&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot;\n        &gt;\n        &lt;Button\n            android:id=&quot;@+id\/speech&quot;\n            android:text=&quot;Speech&quot;\n            android:layout_width=&quot;wrap_content&quot;\n            android:layout_weight=&quot;1&quot;\n            android:layout_height=&quot;wrap_content&quot;\n            \/&gt;\n        &lt;Button\n            android:id=&quot;@+id\/record&quot;\n            android:text=&quot;Record&quot;\n            android:layout_weight=&quot;1&quot;\n            android:layout_width=&quot;wrap_content&quot;\n            android:layout_height=&quot;wrap_content&quot;\n            \/&gt;\n    &lt;\/LinearLayout&gt;\n\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4e2d\u6587\uff0c<code>int result = textToSpeech.setLanguage(Locale.CHINA);<\/code>\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u66f4\u6539\u4e3a\u5176\u4ed6\u652f\u6301\u7684\u8bed\u8a00\u3002<\/p>\n<p>\u6700\u540e\u5728AndroidManifest.xml\u4e2d\u52a0\u5165\u6743\u9650\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;\/&gt;\n&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;\/&gt;<\/code><\/pre>\n<p>\u603b\u7ed3\uff1a\u901a\u8fc7\u4f7f\u7528Android\u63d0\u4f9b\u7684TTS\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6307\u5b9a\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u6717\u8bfb\uff0c\u4ece\u800c\u53d1\u751f\u58f0\u97f3\uff1b\u8fd8\u5141\u8bb8\u628a\u6587\u672c\u5bf9\u5e94\u7684\u97f3\u9891\u5f55\u5236\u6210\u97f3\u9891\u6587\u4ef6\uff0c\u4fdd\u5b58\u5230\u672c\u5730\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u63d0\u4f9b\u4e86\u81ea\u52a8\u6717\u8bfb\u652f\u6301\u3002\u53ef\u4ee5\u5bf9\u6307\u5b9a\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u6717\u8bfb\uff0c\u4ece\u800c\u53d1\u751f\u58f0\u97f3\uff1b\u8fd8\u5141\u8bb8\u628a\u6587\u672c\u5bf9\u5e94\u7684\u97f3\u9891\u5f55\u5236\u6210\u97f3\u9891\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[318],"class_list":["post-1273","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-tts"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1273"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1273\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}