Playing audio requires cordova.js in browser

I’m trying to play an audio for all the platforms, but in browser I got the following message:

common.js:284 Native: tried calling, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator

The solutions I found in google says that I should use the following comand:

  • ionic cordova run browser

But I’m using Capacitor and it refuses to use the command inside the Capacitor project.

My question is.

Do I need to make another implementation to play audio in browsers or I can just keep things this way and use some kind of command to run this NativeAudio in browsers?

More information:
The library I’m using is: @ionic-native/native-audio/ngx
The method I’mCalling is…)

I would encourage you to look at this from the opposite direction.

Anything you can do using only portable browser-supported HTML features, I would suggest doing that way, if only for the fact that it simplifies everything and means you don’t have to ask questions like this and can spend your time doing more interesting things.

Ergo, if I were going to play audio from an Ionic app, I would use the <audio> element and MediaStreams in general.

If you decide to go that way, I think we’re done here.

If you’re still reading, any time you deal with something called “Native” anything, it’s best to assume that:

  • it’s only going to work on a device (or possibly an emulator)
  • that means no rapid development using a desktop browser
  • support may be limited to certain OS flavors and versions
  • you are committing yourself to a lot more future busywork dealing with device compatibility

Cordova (and Capacitor) are fundamentally message brokers. They pass messages back and forth across a bridge between JavaScript running in a browser and code that is compiled in some other language running in a native mobile OS. These days, that basically means “iOS and/or Android” - historically, there were BlackBerries, Windows Phones, and so on.

The Cordova “browser” platform is merely a fugazi “mobile OS”, where the “native” side of the bridge isn’t really native, but just more JavaScript trying to do its best to fake whatever the functionality is.

Here, the path forks.

Down one road is stuff that is simply impossible to fake - HealthKit / Google Fit, for example. Can’t be done in a browser, no matter how loudly you yell and how vigorously you wave your arms.

Down the other path is stuff that can be reasonably done within a modern web browser using HTML5 - streaming media, geolocation, and so on. The ironic thing is that if you’re looking at something in this neighborhood, there wasn’t really any point in resorting to Cordova in the first place.

To summarize,

  • the Cordova “browser” platform is largely pointless
  • even if it wasn’t pointless, it’s hard to use and doesn’t work seamlessly the way you seem to expect it would
  • “native” functionality imposes significant hassle on your app development, so it’s best reserved for situations where there is no portable way to do what you want
  • playing audio is a situation where there is a portable way to do what you want, so I would suggest doing that instead, and forgetting completely that NativeAudio exists
1 Like

Thanks for the fantastic answer. Now I got it.