简体   繁体   English

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

[英]How do i implement the text to speech code in my new project?

The text to speech code i was doing it in Eclipse and it's working there great. 文本到语音的代码我是在Eclipse中完成的,并且在那方面运行良好。 But now i created a new bigger project in Android Studio and i want to add the Text To Speech code to this project. 但是现在我在Android Studio中创建了一个新的更大项目,并且我想将Text To Speech代码添加到该项目中。

Both project and the code are in java. 项目和代码都在Java中。

This is the Text To Speech code: 这是“文字转语音”代码:

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. Should i add this code somehow to my MainActivity.java in my project in Android Studio ? 我是否应该将此代码以某种方式添加到Android Studio项目中的MainActivity.java中?

  2. Maybe i should create a new class and somehow to implement the Text To Speech code there ? 也许我应该创建一个新类,并以某种方式在此处实现“文字转语音”代码?

This is my MainActivity.java code now: 现在是我的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();
    }
}

And this is the code of TextToSpeechServer and from there i want to be able to call and use the Text To Speech code: 这是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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM