flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

synthesizeToFile saves in inconvenient path location

Open michelesandroni opened this issue 3 years ago • 6 comments

synthesizeToFile() currently saves files into: /storage/emulated/0/Android/data/com.example.myapp/files/ which is what is returned for example by path_provider using getExternalStorageDirectory(), which can be null on iOS.

Here's a comment from path_provider.dart

/// Path to a directory where the application may access top level storage.
/// The current operating system should be determined before issuing this
/// function call, as this functionality is only available on Android.
///
/// On iOS, this function throws an [UnsupportedError] as it is not possible
/// to access outside the app's sandbox.
///
/// On Android this uses the getExternalFilesDir(null).
Future<Directory?> getExternalStorageDirectory() async {

Wouldn't it be better to save files inside the app sandbox using getApplicationDocumentsDirectory() ?

/// Path to a directory where the application may place data that is
/// user-generated, or that cannot otherwise be recreated by your application.
///
/// On iOS, this uses the NSDocumentDirectory API. Consider using
/// [getApplicationSupportDirectory] instead if the data is not user-generated.
///
/// On Android, this uses the getDataDirectory API on the context. Consider
/// using [getExternalStorageDirectory] instead if data is intended to be visible
/// to the user.
///
/// Throws a MissingPlatformDirectoryException if the system is unable to
/// provide the directory.
Future<Directory> getApplicationDocumentsDirectory() async {

Here are some informations about changes in Android 11 and external storage https://developer.android.com/about/versions/11/privacy/storage

michelesandroni avatar Jul 10 '21 06:07 michelesandroni

@mchlsndrn I plan on allowing the user to provide a full path to an existing directory so this plugin can write directly to a file at that location.

dlutton avatar Jul 10 '21 21:07 dlutton

Good idea. Btw where does it save files on iOS?

michelesandroni avatar Jul 11 '21 13:07 michelesandroni

in iOS something like this com.tundralabs.example/Data/Documents/tts.caf

dlutton avatar Jul 11 '21 18:07 dlutton

@dlutton sorry to ask again but this might cause additional headaches later on.

Currently files are synthetized to getExternalStorageDirectory(). fI later you improve this and add the ability to provide a path parameter, I will still have to access getExternalStorageDirectory() in order to list / playback previously saved mp3 files.

Do you have any ETA for this?

michelesandroni avatar Jan 16 '22 23:01 michelesandroni

@mchlsndrn that is the plan, however I don't have an ETA as of right now since I've been extremely busy, but hopefully I can get to it soon.

dlutton avatar Jan 18 '22 01:01 dlutton

in iOS something like this com.tundralabs.example/Data/Documents/tts.caf

@dlutton I just tested my app on a mac and I'm not sure what your path corresponds using get*Directory() calls from path_provider.

I tried listing files in "/com.mycompany.example/Data/Documents" but I'm getting errors

flutter: Directory "/com.mycompany.example/Data/Documents/" not found.
flutter: Directory "/com.mycompany.example/Data/Documents" not found.

According to my test, the files are saved in getApplicationDocumentsDirectory().path Anyways, I want to list all audio files saved in this folder which is where a bunch of other stuff gets saved by other packages (including hive databases, for example). I'm simply filtering files using the extension .caf (on iOS) and .wav (on Android) but the improvement you suggested woould be better and probably shouldn't be extremely difficult to implement.

michelesandroni avatar May 29 '22 22:05 michelesandroni

Closing. see #443

michelesandroni avatar Dec 10 '23 20:12 michelesandroni