我如何在我的新项目中实现文本到语音代码



文本到语音代码我是在Eclipse中做的,它在那里工作得很好。但是现在我在Android Studio中创建了一个新的更大的项目,我想将文本到语音代码添加到这个项目中。

项目和代码都是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);
                    }
        }
    }
}
  1. 我应该把这个代码添加到我的MainActivity.java在我的项目在Android Studio ?

  2. 也许我应该创建一个新的类,并以某种方式实现文本到语音代码?

这是我现在的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的代码,从那里我想要能够调用和使用文本到语音代码:

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

从Eclipse项目迁移到Android Studio:- http://tools.android.com/tech-docs/new-build-system/migrating-from-eclipse-projects

详细文档:https://developer.android.com/sdk/installing/migrate.html

最新更新