Problem with Ionic 6.5.0 – NativeAudio – MP3 file not found – FileNotFoundException

Whatever I tried I always got this error

Since I spent several hours and just couldn’t get the NativeAudio plugin to play an audio file in Ionic (no matter if React or Angular), I had to dig through the JAVA code of the plugin.

Apparently the Cordova NativeAudio plugin is not compatible with the new Capacitor directory structure ?

Therefore make the following adjustment in this file:

appname/android/capacitor-cordova-android-plugins/src/main/java/com/rjfun/cordova/plugin/nativeaudio/NativeAudio.java

try {
        audioID = data.getString(0);
        if (!assetMap.containsKey(audioID)) {
                String assetPath = data.getString(1);
                Log.d(LOGTAG, "preloadComplex - " + audioID + ": " + assetPath);

                double volume;
                if (data.length() <= 2) {
                        volume = 1.0;
                } else {
                        volume = data.getDouble(2);
                }

                int voices;
                if (data.length() <= 3) {
                        voices = 1;
                } else {
                        voices = data.getInt(3);
                }

                String fullPath = "www/".concat(assetPath);

                Context ctx = cordova.getActivity().getApplicationContext();
                AssetManager am = ctx.getResources().getAssets();
                AssetFileDescriptor afd = am.openFd(fullPath);

                NativeAudioAsset asset = new NativeAudioAsset(
                                afd, voices, (float)volume);
                assetMap.put(audioID, asset);

                return new PluginResult(Status.OK);
        } else {
                return new PluginResult(Status.ERROR, ERROR_AUDIOID_EXISTS);
        }
} catch (JSONException e) {
        return new PluginResult(Status.ERROR, e.toString());
} catch (IOException e) {
        return new PluginResult(Status.ERROR, e.toString());
}

In the marked line simply change “www” by “public”.

Then

ionic capacitor run android --host=IP_DEINES_RECHNERS --livereload

To view the changes in the app on your phone.

From now on the NativeAudio plugin finds the files.

For the first time after hours the code gave me a “Y” as an alert box ?

import { Component } from '@angular/core';
import { NativeAudio } from '@ionic-native/native-audio/ngx';
import { Platform } from '@ionic/angular';


@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {


  constructor(

    public platform: Platform 

  ) {


    this.platform.ready().then(() => { 

        var n = new NativeAudio;

        var onSuccess = function() {
          alert("Y");
        }
    
        var onError = function(u: any) {
          alert("X2" + u);
        } 
    
        n.preloadSimple('uniqueId12', 'assets/file2.mp3').then(onSuccess, onError);
     

        //alert("platform ready");
    });

 

  }

  
  playSound() {
    

  
    alert("test");   
  }


}

 

Problem with Ionic 6.5.0 – NativeAudio – MP3 file not found – FileNotFoundException

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top