1

I have used

tts.addSpeech(key_string, path);

to map an audio file to a string of text, with for example

key_string = "Android"

and

path = "/storage/emulated/0/Android/data/com.example.admin.machinelearning/files /associations/androidcalm.wav".

After calling the following method on Android Api level 17

tts.speak(key_string, queueMode, params);

everything works great and the mapped sound file is played.

But calling the above method on Android Api level 23, I'm getting the following error,

02-15 11:08:25.941 7220-19682/? W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content   provider: /storage/emulated/0/Android/data/com.example.admin.machinelearning/files/associations/androidcalm.wav
02-15 11:08:25.946 7220-19682/? D/MediaPlayer: create failed:
                                            java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.example.admin.machinelearning/files/associations/androidcalm.wav: open failed: EACCES (Permission denied)
                                               at libcore.io.IoBridge.open(IoBridge.java:452)
                                               at java.io.FileInputStream.<init>(FileInputStream.java:76)
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1122)
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1094)
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1048)
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:986)
                                               at android.media.MediaPlayer.create(MediaPlayer.java:893)
                                               at android.speech.tts.AudioPlaybackQueueItem.run(AudioPlaybackQueueItem.java:58)
                                               at android.speech.tts.AudioPlaybackHandler$MessageLoop.run(AudioPlaybackHandler.java:134)
                                               at java.lang.Thread.run(Thread.java:818)
                                            Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
                                               at libcore.io.Posix.open(Native Method)
                                               at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                               at libcore.io.IoBridge.open(IoBridge.java:438)
                                               at java.io.FileInputStream.<init>(FileInputStream.java:76) 
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1122) 
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1094) 
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1048) 
                                               at android.media.MediaPlayer.setDataSource(MediaPlayer.java:986) 
                                               at android.media.MediaPlayer.create(MediaPlayer.java:893) 
                                               at android.speech.tts.AudioPlaybackQueueItem.run(AudioPlaybackQueueItem.java:58) 
                                               at android.speech.tts.AudioPlaybackHandler$MessageLoop.run(AudioPlaybackHandler.java:134) 
                                               at java.lang.Thread.run(Thread.java:818) 

I made sure that the sound file exists and is playable by using the code below,

Uri uri = Uri.parse(new File("/storage/emulated/0/Android/data/com.example.admin.machinelearning/files/associations/androidcalm.wav").toString());                            
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getActivity(),uri);
mediaPlayer.prepare();
mediaPlayer.start();

which successfully plays the sound file.

Also, I've tried the code below (on both of the android api levels above) to read a text file and then speak the characters contained in it

String toSay = "Error";

try {
               InputStream in = new FileInputStream("/storage/emulated/0/Android/data/com.example.admin.machinelearning/files/associations/x.txt");
                toSay = FileUtil.getContentsOfReader(new BufferedReader(new InputStreamReader(in)));
            } catch (IOException e) {
                e.printStackTrace();
            }

then

tts.speak(toSay, queueMode, params);

Which is working correctly and the text with in the text file is spoken.

First: If this Is a matter of Reading permissions, then why I'm not getting the same error here?

Second: I tried to add Read and Write permissions on the run (inside a PreferenceFragment), using the code below. "Granted" is getting displayed, but still nothing changed (I'm still getting the error above).

private static final int PERMISSION_REQUEST = 0; 

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE ,   Manifest.permission.WRITE_EXTERNAL_STORAGE},
            PERMISSION_REQUEST);                                                           

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
         if (requestCode == PERMISSION_REQUEST) {
              if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    tts.addSpeech("Android", "/storage/emulated/0/Android/data/com.example.admin.machinelearning/files/associations/androidcalm.wav");
           tts.speak("Android", queueMode, params);
           Toast.makeText(getActivity(),"Granted",Toast.LENGTH_LONG).show();
        } else {
            getActivity().finish();
           Toast.makeText(getActivity(),"Denied",Toast.LENGTH_LONG).show();
        }
    }
  }

Also in Manifest I have declared the following permissions

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>

I'm new to android, maybe I'm doing a silly mistake or didn't understand how this really work, so any help is appreciated.

Note: Testing is done on real android devices.

Brainnovo
  • 1,749
  • 2
  • 12
  • 17

0 Answers0