You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2021/01/21 11:59:39 UTC

[GitHub] [cordova-plugin-media] sefrem commented on issue #265: Can't seem to get Cordova Media to work in Capacitor & Ionic React

sefrem commented on issue #265:
URL: https://github.com/apache/cordova-plugin-media/issues/265#issuecomment-764595279


   @johnsonzsong 
   Hey. Not sure if you still need this but here is how i handled this case in React.
   Here is my helper function 
   
   ```
   export const useSound = (name) => {
     const path = File.applicationDirectory + `public/static/sounds/${name}.mp3`;
     let sound;
     if (Media) {
       sound = Media.create(path);
     }
   
     sound.onStatusUpdate.subscribe(status => {
       if (status === 4) sound.release();
     })
   
     return { sound }
   }
   ```
   
   Then in the component i just pass the name of the sound i want to play and play it like so
   
   ```
   const { sound } = useSound('Example');
   sound.play();
   ```
   One thing that gave me the most pain is setting the proper file path. Here is how File.applicationDirectory works - https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/webkit/URLUtil.java
   
   > to refer to bar.png under your package’s asset/foo/ directory, use
   >  “file:///android_asset/foo/bar.png”.
   
   So basically File.applicationDirectory gives you the path to the assets directory in your project and then you go from there.


----------------------------------------------------------------
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.

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



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