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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]