我在Eclipse中使用文本到语音代码,并且它在那里工作很棒。 但是现在我在Android Studio中创建了一个更大的新项目,我想在这个项目中添加Text To Speech代码。
项目和代码都在java中。
这是文字转语音代码:
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;
import java.util.Locale;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import fi.iki.elonen.NanoHTTPD;
public class MainActivity extends ActionBarActivity implements OnInitListener {
private static final int MY_DATA_CHECK_CODE = 0;
TextToSpeech mTts;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
//tts = new TextToSpeech(this,(OnInitListener) this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTTS();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initTTS() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == MY_DATA_CHECK_CODE) {
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
mTts = new TextToSpeech(this, this);
} else {
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
@SuppressWarnings("deprecation")
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if(result == TextToSpeech.LANG_AVAILABLE
|| result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
mTts.setPitch(1);
mTts.speak("this is a voice test", TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
我应该在Android Studio的项目中以某种方式将此代码添加到我的MainActivity.java中吗?
也许我应该创建一个新类,并以某种方式在那里实现Text To Speech代码?
这是我的MainActivity.java代码:
package com.adi.webservertest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Dispatch onStart() to all fragments. Ensure any created loaders are
* now started.
*/
@Override
protected void onStart()
{
super.onStart();
TextToSpeechServer.main(null);
}
@Override
protected void onStop() {
super.onStop();
}
}
这是TextToSpeechServer的代码,从那里我希望能够调用和使用Text To Speech代码:
package com.adi.webservertest;
import java.util.Map;
/**
* An example of subclassing NanoHTTPD to make a custom HTTP server.
*/
public class TextToSpeechServer extends NanoHTTPD {
public TextToSpeechServer() {
super(8080);
}
@Override public Response serve(IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
System.out.println(method + " '" + uri + "' ");
String msg = "<html><body><h1>Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null)
msg +=
"<form action='?' method='get'>\n" +
" <p>Your name: <input type='text' name='username'></p>\n" +
"</form>\n";
else
msg += "<p>Hello, " + parms.get("username") + "!</p>";
msg += "</body></html>\n";
return new Response(msg);
}
public static void main(String[] args) {
ServerRunner.run(TextToSpeechServer.class);
}
}
答案 0 :(得分:1)
从Eclipse项目迁移到Android Studio: - http://tools.android.com/tech-docs/new-build-system/migrating-from-eclipse-projects
详细文件:https://developer.android.com/sdk/installing/migrate.html