Problem bei Ionic 6.5.0 – NativeAudio – MP3 Datei nicht gefunden FileNotFoundException

Egal was ich probierte es erschien immer dieser Fehler

Da ich mehrere Stunden aufgewendet habe, und es einfach nicht hinbekommen habe, mit dem NativeAudio Plugin eine AudioDatei in Ionic abzuspielen (egal ob React oder Angular), habe ich mich durch den JAVA-Code des Plugins gewühlt.

Anscheinend ist das Cordova NativeAudio Plugin nicht kompatibel mit der neuen Capacitor Verzeichnisstruktur ?

Daher folgende Anpassung in diesem File vornehmen:

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 der markierten Zeile einfach „www“ durch „public“ ändern.

Danach

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

Um die Änderungen in der App auf deinem Smartphone zu sehen.

Ab sofort findet das NativeAudio Plugin die Files.

Zum ersten mal nach Stunden gab der Code ein „Y“ als Alert aus ?

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 bei Ionic 6.5.0 – NativeAudio – MP3 Datei nicht gefunden FileNotFoundException

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen