Android 安卓TTS(文本到语音)和#x27;s addSpeech()和speak()可以';t使用Google TTS在上面的marshmallow(api 23)的外部存储器中播放声音文件

Android 安卓TTS(文本到语音)和#x27;s addSpeech()和speak()可以';t使用Google TTS在上面的marshmallow(api 23)的外部存储器中播放声音文件,android,file,text-to-speech,android-6.0-marshmallow,Android,File,Text To Speech,Android 6.0 Marshmallow,您可能知道,通过android TTS中的addSpeech(),您可以将特定文本链接到声音文件。然后,tts引擎播放文件,而不是合成文本的声音 使用上面的Marshmallow(api 23)中的Google TTS(而非三星TTS),addSpeech&speak无法在外部存储器中播放文件。在棉花糖出现之前,一切都很好 那些使用过android tts和addSpeech的用户,请查看下面的代码并帮助我找到问题所在 代码最初来自,我修改了它们以证明我的观点 写入外部存储权限在清单中,Tar

您可能知道,通过android TTS中的addSpeech(),您可以将特定文本链接到声音文件。然后,tts引擎播放文件,而不是合成文本的声音

使用上面的Marshmallow(api 23)中的Google TTS(而非三星TTS),addSpeech&speak无法在外部存储器中播放文件。在棉花糖出现之前,一切都很好

那些使用过android tts和addSpeech的用户,请查看下面的代码并帮助我找到问题所在

  • 代码最初来自,我修改了它们以证明我的观点
  • 写入外部存储权限在清单中,TargetSDK设置为22以避免棉花糖运行时权限问题
  • 您需要将boy.mp3放在设备的下载文件夹中。 ()
  • 该代码在低于22的设备和三星TTS中运行良好。从23岁以上到谷歌TTS都不起作用
  • 如果我将文件保存在内部存储器中(模式为“世界可读”),即使使用Google TTS也可以正常工作。但是现在不推荐使用模式“世界可读”,所以我更喜欢外部存储

  • 谢谢你的关注,祝你今天愉快

MainActivity.java

package com.woojung.addspeechex;

import android.app.Activity;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.Locale;

public class MainActivity extends Activity {
    TextToSpeech t1;
    EditText ed1;
    Button b1, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1 = (EditText) findViewById(R.id.editText);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);

        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = ed1.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = "boy";
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();

                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File destinationPath = new File(dir, "boy.mp3");

                MediaScannerConnection.scanFile(MainActivity.this, new String[]{destinationPath.toString()}, null, null);

                t1.addSpeech(toSpeak, destinationPath.toString());
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }

    public void onPause() {
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onPause();
    }
}

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:transitionGroup="true">

    <TextView android:text="Text to Speech" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:textSize="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tutorials point"
        android:id="@+id/textView"
        android:layout_below="@+id/textview"
        android:layout_centerHorizontal="true"
        android:textColor="#ff7aff24"
        android:textSize="35dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:theme="@style/Base.TextAppearance.AppCompat" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="46dp"
        android:hint="Enter Text"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#ff7aff10"
        android:textColorHint="#ffff23d1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Synthesize the editText."
        android:id="@+id/button1"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AddSpeech and play boy.mp3"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.woojung.addspeechex">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:transitionGroup="true">

    <TextView android:text="Text to Speech" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:textSize="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tutorials point"
        android:id="@+id/textView"
        android:layout_below="@+id/textview"
        android:layout_centerHorizontal="true"
        android:textColor="#ff7aff24"
        android:textSize="35dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:theme="@style/Base.TextAppearance.AppCompat" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@+id/imageView"
        android:layout_marginTop="46dp"
        android:hint="Enter Text"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#ff7aff10"
        android:textColorHint="#ffff23d1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Synthesize the editText."
        android:id="@+id/button1"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AddSpeech and play boy.mp3"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.woojung.addspeechex">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


可能是时间问题吗?我看到
addSpeech()
是通过按下按钮触发的,但是如果您很快,您可能会在TTS引擎初始化之前触发它。(即在调用
onInit()
之前)@MarkusKauppinen这可能是,考虑到MediaPlayer的prepare()需要时间。我用下面的计时器包装了speak(),但仍然没有运气。(在我的实际项目中,addSpeech是以onResume方式在speak()之前完成的。无论如何,谢谢。new Timer()。schedule(new TimerTask(){(a)override public void run(){t1.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);},2000);你解决过这个问题吗?@brandall No.你解决过吗?我要求谷歌在下面的链接中检查这个问题。如果你想在api级别解决这个问题,请支持我。这可能是一个时间问题吗?我看到
addSpeech()
是通过按下按钮触发的,但是如果您很快,您可能会在TTS引擎初始化之前触发它。(即在调用
onInit()
之前)@MarkusKauppinen,考虑到MediaPlayer的prepare()需要时间。我用下面的计时器包装了speak(),但仍然没有运气。(在我的实际项目中,addSpeech是以onResume方式在speak()之前完成的。无论如何,谢谢。new Timer()。schedule(new TimerTask(){(a)override public void run(){t1.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);},2000);你解决过这个问题吗?@brandall没有。你解决过吗?我要求谷歌在下面的链接中检查这个问题。如果你想在api级别解决这个问题,请支持我。