Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文本到语音在android中不起作用_Android_Text To Speech - Fatal编程技术网

文本到语音在android中不起作用

文本到语音在android中不起作用,android,text-to-speech,Android,Text To Speech,向大师们问好。我目前正在开发一个类似于“待办事项列表”的应用程序。我已经成功实现了获取通知的功能。我试图实现文本到语音转换,使我的索尼Xperia Tipo Dual ST21i2在预先指定的时间说出我添加的任务。但是我的电话里什么也没听到 public class NotifyService extends Service implements OnInitListener{ private TextToSpeech tts; int task_id; private

向大师们问好。我目前正在开发一个类似于“待办事项列表”的应用程序。我已经成功实现了获取
通知的功能
。我试图实现文本到语音转换,使我的
索尼Xperia Tipo Dual ST21i2
在预先指定的时间说出我添加的任务。但是我的电话里什么也没听到

public class NotifyService extends Service implements OnInitListener{

    private TextToSpeech tts;
    int task_id;
    private static final int NOTIFICATION = 123;
    public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
    private NotificationManager mNM;
    SQLiteDatabase database;
    String tmp_task_brief = null;

    public class ServiceBinder extends Binder
    {   
        NotifyService getService()
        {
            return NotifyService.this;
        }
    }

    @Override
    public void onCreate() {

        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        tts = new TextToSpeech(getApplicationContext(), this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        task_id = intent.getIntExtra("task_id", 0);

        loadDatabase();
        Cursor cursor = database.query("task_info", new String[]{"task_brief"}, "task_id=?", new String[]{task_id+""}, null, null, null);
        while(cursor.moveToNext())
        {
            tmp_task_brief = cursor.getString(0);
        }
        cursor.close();
        database.close();

        if(intent.getBooleanExtra(INTENT_NOTIFY, false))
            showNotification(tmp_task_brief);

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    private final IBinder mBinder = new ServiceBinder();

    private void showNotification(String tmp_task_brief) {

        CharSequence title = "To Do Task Notification!!";
        int icon = R.drawable.ic_menu_notifications;
        CharSequence text = tmp_task_brief;     
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);

        notification.setLatestEventInfo(this, title, text, contentIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        mNM.notify(NOTIFICATION, notification);

        //---------vibrate on notification-----------

        Vibrator vibrate = (Vibrator)   getSystemService(Context.VIBRATOR_SERVICE);

        int dot = 200;
        int dash = 500;
        int short_gap = 200;
        int medium_gap = 500;
        int long_gap = 1000;
        long[] pattern = {
            0,  // Start immediately
            dot, short_gap, dot, short_gap, dot,    // s
            medium_gap,
            dash, short_gap, dash, short_gap, dash, // o
            medium_gap,
            dot, short_gap, dot, short_gap, dot,    // s
            long_gap
        };

        // Only perform this pattern one time (-1 means "do not repeat")
        vibrate.vibrate(pattern, -1);

        speakOut(tmp_task_brief);

        stopSelf();
    }

    void loadDatabase()
    {
        database = openOrCreateDatabase("ToDoDatabase.db",
                SQLiteDatabase.OPEN_READWRITE, null);
    }

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.i("TTS", "This Language is not supported");
                Toast.makeText(getApplicationContext(), "This Language is not supported", Toast.LENGTH_SHORT).show();
            } else {
                speakOut(tmp_task_brief);
            }

        } else {
            Log.i("TTS", "Initilization Failed!");
        }
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    private void speakOut(String task) {

        tts.speak(task, TextToSpeech.QUEUE_FLUSH, null);
    }
}
我将输出错误记录如下:

05-31 11:49:00.450: I/TextToSpeech(11621): Sucessfully bound to com.google.android.tts
05-31 11:49:00.490: W/TextToSpeech(11621): speak failed: not bound to TTS engine
05-31 11:49:00.490: I/TextToSpeech(11621): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.GoogleTTSService}
请帮助我解决此错误。

在speak方法有机会发言之前调用
stopSelf()
。您应该实现
onutternancecompletedlistener
并在
onutternancecompleted
内部调用
stopSelf()

此外,
speakOut(tmp任务摘要)showNotification
方法中调用code>,因为
speak
方法仅在调用
onInit
后才起作用。

我在处理文本到语音的过程中遇到了同样的问题。当您在设备上禁用Google文本到语音时,就会发生这种情况。
尝试将谷歌文本更新为语音或启用它。

谢谢您,先生。我实现了我想要的。我想问你一件事。我已经在这个应用程序中实现了语音输入功能。我需要数据连接吗,语音到文本才能工作?您需要互联网连接才能语音到文本工作。但是,可以使用createSoundFile创建声音文件。之后你就不需要上网了。听起来不错。请你给我提供一些例子,链接或教程。请,嗯。再次感谢。很好的教程。我一定会努力实现这一点。