Description
react-native-sound@0.11.2 supported React Native static asset IDs, for example:
import Sound from 'react-native-sound';
const sound = new Sound(require('./beep.mp3'), (error) => {
if (!error) {
sound.play();
}
});
Since 0.12.0, this can crash at runtime because the constructor assumes filename is a string:
if (filename.startsWith("http")) {
In React Native, require('./beep.mp3') / static asset imports resolve to a numeric asset ID. In 0.11.2, sound.js handled this with:
var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
var asset = resolveAssetSource(filename);
if (asset) {
this._filename = asset.uri;
onError = basePath;
}
That asset-resolution path disappeared in the TypeScript rewrite / new architecture work.
Observed behavior
The app crashes when constructing Sound with a static asset:
TypeError: undefined is not a function
at Sound
Depending on engine/minification this points at the filename.startsWith(...) call.
Expected behavior
Static RN assets should either keep working as before, or the TypeScript/API docs should clearly reject numeric asset IDs. The current typings/documentation still imply require(...) assets are supported.
Regression
Works in: react-native-sound@0.11.2
Broken in: react-native-sound@0.12.0 and 0.13.0
Relevant diff:
- var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
-
function Sound(filename, basePath, onError, options) {
- var asset = resolveAssetSource(filename);
- if (asset) {
- this._filename = asset.uri;
- onError = basePath;
- } else {
- this._filename = basePath ? basePath + '/' + filename : filename;
- }
+ if (filename.startsWith("http")) {
+ this._filename = filename;
+ } else {
+ this._filename = basePath ? `${basePath}/${filename}` : filename;
+ }
}
Possible fix
Restore asset resolution before string handling, e.g. resolve numeric/static assets with React Native's asset resolver and only call string methods after narrowing filename to a string.
Description
react-native-sound@0.11.2supported React Native static asset IDs, for example:Since
0.12.0, this can crash at runtime because the constructor assumesfilenameis a string:In React Native,
require('./beep.mp3')/ static asset imports resolve to a numeric asset ID. In0.11.2,sound.jshandled this with:That asset-resolution path disappeared in the TypeScript rewrite / new architecture work.
Observed behavior
The app crashes when constructing
Soundwith a static asset:TypeError: undefined is not a function at SoundDepending on engine/minification this points at the
filename.startsWith(...)call.Expected behavior
Static RN assets should either keep working as before, or the TypeScript/API docs should clearly reject numeric asset IDs. The current typings/documentation still imply
require(...)assets are supported.Regression
Works in:
react-native-sound@0.11.2Broken in:
react-native-sound@0.12.0and0.13.0Relevant diff:
Possible fix
Restore asset resolution before string handling, e.g. resolve numeric/static assets with React Native's asset resolver and only call string methods after narrowing
filenameto a string.