Android:NDK:超级打开失败:ENOENT(没有这样的文件或目录)错误

Posted

技术标签:

【中文标题】Android:NDK:超级打开失败:ENOENT(没有这样的文件或目录)错误【英文标题】:Android : NDK : Superpowered Open failed: ENOENT (No such file or directory) Error 【发布时间】:2021-03-26 01:42:22 【问题描述】:

您好,我正在尝试使用 Superpowered::AdvancedAudioPlayer 从内部存储中播放 audio.wav,但 NDK 总是通过 Open failed: ENOENT (No such file or directory) 异常。 代码:

//-----------------Java--------------- -------------------------------------------------- ----

 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
        String myfile = path + "filename" + ".mp3";



if (samplerateString == null) samplerateString = "48000";
        if (buffersizeString == null) buffersizeString = "480";
         samplerate = Integer.parseInt(samplerateString);
         buffersize = Integer.parseInt(buffersizeString);

//-----------------Cpp--------------- -------------------------------------------------- -------

SoundEffects1(samplerate,     // sampling rate
                        buffersize,     // buffer size
                        myfile,        // path to .apk package
                        fileAoffset,    // offset (start) of file A in the APK
                        fileAlength,    // length of file A
                        fileBoffset,    // offset (start) of file B in the APK
                        fileBlength,     // length of file B
                        myfile,
                        myfile
            );

Extern "C"
JNIEXPORT void JNICALL
Java_com_example_superpoweredandroidtest_Dashboard_SoundEffects1(
        JNIEnv *env,
        jobject __unused ob,
        jint samplerate,
        jint buffersize,
        jstring apkPath,
        jint fileAoffset,
        jint fileAlength,
        jint fileBoffset,
        jint fileBlength,
        jstring file1Path,
        jstring file2Path
)
    const char *path = env->GetStringUTFChars(apkPath, JNI_FALSE);

    const char *file1Name = env->GetStringUTFChars(file1Path, JNI_FALSE);
    const char *file2Name = env->GetStringUTFChars(file2Path, JNI_FALSE);


    __android_log_print(ANDROID_LOG_DEBUG, APPNAME, "SoundEffects JNI Constructor %s",file1Name);
    soundEffects = new SoundEffects((unsigned int)samplerate, (unsigned int)buffersize,
                                    path, fileAoffset, fileAlength, fileBoffset, fileBlength,file1Name,file2Name);

 /*   env->ReleaseStringUTFChars(file1Path, file1Name);
    env->ReleaseStringUTFChars(file2Path, file2Name);
 */   env->ReleaseStringUTFChars(apkPath, path);




SoundEffects::SoundEffects (
        unsigned int samplerate, // device native sample rate
        unsigned int buffersize, // device native buffer size
        const char *path,        // path to APK package
        int fileAoffset,         // offset of file A in APK
        int fileAlength,         // length of file A
        int fileBoffset,         // offset of file B in APK
        int fileBlength,
        const char *file1Path,
        const char *file2Path
) : activeFx(0), numPlayersLoaded(0), crossFaderPosition(0.0f), volB(0.0f), volA(1.0f * headroom)


    initializeSDK();

    playerA = new Superpowered::AdvancedAudioPlayer(samplerate, 0);
    playerB = new Superpowered::AdvancedAudioPlayer(samplerate, 0);
    roll = new Superpowered::Roll(samplerate);
    filter = new Superpowered::Filter(Superpowered::Resonant_Lowpass, samplerate);
    flanger = new Superpowered::Flanger(samplerate);
    filter->resonance = 0.1f;


  //  stereoBuffer = (float *) memalign(16, (buffersize + 16) * sizeof(float) * 2);


  //  __android_log_print(ANDROID_LOG_DEBUG, APPNAME, "file===> %s",path);



//----------------------------------------------------------------------------------------------
    std::fopen(path, "r");

    __android_log_print(ANDROID_LOG_DEBUG, APPNAME, "SoundEffects Cpp Constructor %s",path);
      playerA->open(path);
 //   playerB->open(path);

    playerA->play();
 //-------------------working---------------------------------------------------------------------------



    //     playerA->open(path, fileAoffset, fileAlength);
//        playerB->open(path, fileBoffset, fileBlength);


    // Initialize audio engine and pass callback function.
    output = new SuperpoweredAndroidAudioIO (
            samplerate,                     // device native sample rate
            buffersize,                     // device native buffer size
            false,                          // enableInput
            true,                           // enableOutput
            audioProcessing,                // audio callback function
            this,                           // clientData
            -1,                             // inputStreamType (-1 = default)
            SL_ANDROID_STREAM_MEDIA         // outputStreamType (-1 = default)
    );



void SoundEffects::initializeSDK() const 
    Superpowered::Initialize(
            "ExampleLicenseKey-WillExpire-OnNextUpdate",
            false, // enableAudioAnalysis (using SuperpoweredAnalyzer, SuperpoweredLiveAnalyzer, SuperpoweredWaveform or SuperpoweredBandpassFilterbank)
            false, // enableFFTAndFrequencyDomain (using SuperpoweredFrequencyDomain, SuperpoweredFFTComplex, SuperpoweredFFTReal or SuperpoweredPolarFFT)
            false, // enableAudioTimeStretching (using SuperpoweredTimeStretching)
            true,  // enableAudioEffects (using any SuperpoweredFX class)
            true,  // enableAudioPlayerAndDecoder (using SuperpoweredAdvancedAudioPlayer or SuperpoweredDecoder)
            false, // enableCryptographics (using Superpowered::RSAPublicKey, Superpowered::RSAPrivateKey, Superpowered::hasher or Superpowered::AES)
            false  // enableNetworking (using Superpowered::httpRequest)
    );

【问题讨论】:

getExternalStorageDirectory() 不是内部目录。您必须有权访问它。 @codecontract操作系统为他们和他们单独提供。从那以后我一直在睡觉,所以如果我错了,请纠正我。 @NathanToulbert 是的,所有应用都可以在没有权限的情况下访问他们的外部存储目录。然而,在这个问题的上下文中,被请求的目录不是特定于应用程序的,所以我们需要访问它的权限(即使这种行为被弃用:Environment#getExternalStorageDirectory()) 【参考方案1】:

我已经用 C 语言编写了代码来打开和读取文本文件,如下所示,这工作正常。希望您对如何打开文件有所了解

char *myPath;
char *myText;
char *mstrings;
JNIEXPORT jstring JNICALL
Java_com_my_apppackage_utils_Utils_readFileFromCNative(JNIEnv *env,
                                                            jobject instance, jstring path) 

    myPath = (char *) (*env)->GetStringUTFChars(env, path, 0);

    mstrings = readDocument(myPath);

    jstring myString = (*env)->NewStringUTF(env, mstrings);

//    (*env)->ReleaseStringUTFChars(env, path, myPath);

    return myString;

这里是ReadDocument.c

#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include "ReadDocument.h"

#define MY_FILENAME_SIZE 1000

char MY_PATH[MY_FILENAME_SIZE];

FILE *fileReader;

char *readDocument(char *filename) 

    char filePath[MY_FILENAME_SIZE];
    int fileSize;
    char *contents;

    // Open file
    strcat(strcpy(filePath, MY_PATH), filename);
    if ((fileReader = fopen(filePath, "rb")) == NULL) 
        exit(EXIT_FAILURE);
    

    // Get file size
    fseek(fileReader, 0, SEEK_END);
    fileSize = ftell(fileReader);
    rewind(fileReader);

    // Allocate string
    contents = malloc(fileSize + 1); // +1 for termination
    if (contents == NULL) 
        fclose(fileReader);
        free(fileReader);
        printf("Could not malloc contents:");
        exit(EXIT_FAILURE);
    

    // Load dictionary
    fread(contents, fileSize, 1, fileReader);
    contents[fileSize] = 0;

    fclose(fileReader);

    return contents;

【讨论】:

以上是关于Android:NDK:超级打开失败:ENOENT(没有这样的文件或目录)错误的主要内容,如果未能解决你的问题,请参考以下文章

Android上的java.io.FileNotFoundException,打开失败:ENOENT(没有这样的文件或目录)

Android Studio 3.6.3 - 拍照时相机错误:打开失败:ENOENT(没有这样的文件或目录)

java.io.FileNotFoundException:/key.p12:打开失败:ENOENT(没有这样的文件或目录)

Cordova Build Android 失败,退出代码 ENOENT

Appium 错误:Logcat 捕获失败:使用 Android Studio 生成 ENOENT

android目标:android:命令失败,退出代码为enoent Meteor