Skip to content

Regression: static asset IDs crash in Sound constructor since 0.12.0 #895

Description

@wojtekmaj

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions