breautek commented on issue #401:
URL: 
https://github.com/apache/cordova-plugin-media/issues/401#issuecomment-2421176459

   > Where are you getting your information about applicationStorageDirectory 
being read only?
   
   My apologies, I got confused between `applicationStorageDirectory` and 
`applicationDirectory`. You're right that `applicationStorageDirectory` is a 
writable path.
   
   > I understand that using the platform_www directory is not recommended; 
however, unlike iOS, I've never been able to get the files from the www folder 
to update the platform_www when rebuilding the app. Maybe you can enlighten me 
on which step I'm missing, but that's another story and irrelevant to this 
issue.
   
   You should have a `<cordova-project-root>/www` directory that contains your 
index.html and other web assets. Cordova copies this entire directory and 
places it in the platform's assets. For android, that would be inside 
`/platforms/android/app/src/main/assets/`.
   
   > I am using the file-transfer plugin to download from an online web 
location to the device. I can see a file produced on the device when I download 
to externalRootDirectory because apparently the emulated SD card is the 
internal Music directory on my device. 
   
   Android has became much more stricter in attempt to provide greater privacy 
to the end-user. Since API 29 accessing the external file system (e.g. anything 
inside `/storage/emulated/...`) has severe limitations when using the File API. 
Content is suppose to be accesesd via the MediaStore, which Apache does not 
have a plugin for, but there are third-party plugins available. Though I'm not 
sure what happens if the application is installed on the external filesystem/sd 
card. I do believe the application-specific directories on the external storage 
is still freely accessible. On API 29+ device, writing files to 
`externalRootDirectory` should not work, but `applicationStorageDirectory` 
should work.
   
   > However, when trying to play a (missing) file which I never downloaded to 
applicationStorageDirectory, I get a media plugin error that file was not 
found, whereas if I download it first, I get the Logcat error "failed to 
connect to localhost" and an undefined error from the media plugin.
   
   This part I'm not sure if I'm completely understanding.
   
   Suppose your loaded document is at `<cordova-project>/www/index.html`. 
Everything inside the `www` directory becomes app assets, which in the webview 
this maps to `https://localhost/`. I'm not sure on top of my head what the path 
looks like on device, but it leads to a readonly path, where the app install 
is. So keep note of this, we'll come back to this detail in a moment.
   
   It sounds like you're using the file transfer plugin to download a mp3 to 
`applicationStorageDirectory` which is a directory completely outside of the 
app's asset directory. It's on a separate data partition that is writable.
   
   ```javascript
   my_media = new Media("test.mp3", ...
   ```
   
   So now we have this code snippet, that relatively loads in `test.mp3`. This 
will be relative to the loaded documented. So if the loaded document is 
`https://localhost/index.html`, then the above will attempt to load 
`https://localhost/test.mp3` and this is looking for `test.mp3` in the 
read-only assets directory, which wouldn't exist, and is certainly not the same 
directory as `applicationStorageDirectory`.
   
   If you want to use the downloaded file, you'll need to get the FileEntry 
object using the file plugin. With the `FileEntry` you can use `.toURL()` to 
get a DOM-usable URL. The URL will look something like 
`https://localhost/__cdvfile__xxxx/test.mp3`. I've mentioned previously about 
limitations with accessing the external storage with the File API, but as long 
as the file is stored within an app directory (e.g. 
`/storage/emulated/0/<app-id>/...`) then I believe it will still work.
   
   An example/untested code might look like:
   
   ```javascript
   let path = cordova.file.applicationStorageDirectory + "/test.mp3";
   
   window.resolveLocalFilesystemURI(path, function(fileEntry) {
     let media = new Media(fileEntry.toURL());
     ...
   }, onError);
   ```
   
   Let me know if this helps.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to