4

In react-native 0.14, we can require an image using const fireImage = require('./images/demo.jpg'). The require works fine with images(png, jpeg, gif), 'js' and 'json' files.

My question is: Is it possible to 'require' a '.mp3' or '.mp4' files?

I have tried the 'mp3' file and the Xcode reported a build error like this

uncaught error Error: UnableToResolveError: Unable to resolve module ./music/some from /Users/rongshen/tmp/react-native-test/imageTest/index.ios.js: Invalid directory /Users/rongshen/tmp/react-native-test/imageTest/music/some.mp3
user552403
  • 306
  • 1
  • 7

2 Answers2

3

For anyone else running across this post that's still trying to figure it out, you can utilize the rn-cli.config.js file to add additional file types for the require() method.

Example, add a file named rn-cli.config.js to the project root with the following contents:

module.exports = {
    getAssetExts() {
        return ['mp3', 'mp4']
    }
}
shparkison
  • 943
  • 1
  • 8
  • 20
0

If you're trying to play the mp3 file, I would recommend using React Native Sound

Ryan Wu
  • 5,963
  • 2
  • 36
  • 47