title: Android TTS(Text to Speech)'s addSpeech() and speak() can't play a sound file in the external storage from marshmallow(api 23) above, with Google TTS
body: <p>As you might know, with <strong>addSpeech()</strong> in android TTS, you can link a certain text to a sound file. Then, the tts engine plays the file instead of synthesizing the sound of the text.</p>
  
  <p>With Google TTS (not Samsung TTS), from Marshmallow(api 23) above, addSpeech &amp; speak can't play a file in the external storage. Before Marshmallow everything was OK.</p>
  
  <p>Those who have used android tts and addSpeech, please take a look at the codes below and help me find the problem.</p>
  
  <ul>
  <li>The codes were originally from <a href="http://www.java2s.com/Code/Android/Media/MediaPlayerandTexttoSpeech.htm" rel="nofollow noreferrer">http://www.java2s.com/Code/Android/Media/MediaPlayerandTexttoSpeech.htm</a>, and I modified them to prove my point.</li>
  <li>WRITE_EXTERNAL_STORAGE permission is in manifest and TargetSDK is set to 22 to avoid marshmallow runtime permission issues.</li>
  <li>You need to put boy.mp3 in DOWNLOADS folder in your device.
  ( <a href="https://dl.dropboxusercontent.com/u/61874191/boy.mp3" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/61874191/boy.mp3</a> )</li>
  <li>The code works fine in below 22 devices and Samsung TTS. It doesn't work from 23 above and with Google TTS.</li>
  <li><p>If I save the file in the internal storage (with MODE_WORLD_READABLE) it works fine even with Google TTS. But MODE_WORLD_READABLE is now deprecated, so I'd prefer external storage. </p></li>
  <li><p>Thank you for your attention, and have a good day.</p></li>
  </ul>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>build.grade(module)</strong></p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 22
      buildToolsVersion "22.0.1"
      defaultConfig {
          applicationId "com.woojung.addspeechex"
          minSdkVersion 16
          targetSdkVersion 22
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      compile 'com.android.support:appcompat-v7:22.2.0'
  }
  </code></pre>
  
  <hr>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.woojung.addspeechex"&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>