2

我正在尝试通过 JNI 使用 FAAC 为我的 Android 应用程序项目启用 AAC 编码。一切似乎都工作正常,但编码部分是,嗯......相当奇怪。我必须承认我对音频编程并不熟悉,并且已经搜索了几天的解决方案和答案,但还没有找到答案。

情况是,我使用 MediaRecord 将音频录制到 RAW PCM 数据中,并将文件保存到一个临时文件中,比如“temp.pcm”。然后使用下面的代码将其编码为 AAC .m4a 文件。问题是,编码文件已保存,大小看起来不错,但无法被 mPlayer 或任何其他媒体播放器识别。播放它们会出现一些错误,例如不支持的格式。编码文件似乎有一些错误的结构。

我对此一无所知。以前有人试过吗?请分享您的经验或提供一些提示......我对此非常绝望...... :(

编辑 1: 只是想,如下面的代码,我实际上是在获取 m4a 文件的原始数据,但没有标题或其他结构以使玩家无法识别它吗?

java部分:

jint Java_com_phonegap_plugins_cjplugs_CJPlugs_JNIconvPCM2FAAC(
    JNIEnv* env,
    jobject thiz, 
    jstring inputPath, 
    jstring outputPath  )
{
    const char *inFile = (*env)->GetStringUTFChars(env, inputPath, NULL);
    const char *outFile = (*env)->GetStringUTFChars(env, outputPath, NULL);

    return cppJNIconvPCM2FAAC(inFile, outFile);
}

C++ 中的实际 JNI 部分与另一个包装文件桥接:

#include <cerrno>
#include <cstddef>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>

#include "ipaws.h"
#include "faac.h"

int cppJNIconvPCM2FAAC(
    const char *inputPath, 
    const char *outputPath  )
{
    unsigned long faacInputSamples;
    unsigned long faacMaxOutputBytes;

    faacEncHandle faac = faacEncOpen(16000, 1, &faacInputSamples, &faacMaxOutputBytes);
    if ( !faac ) {
        return 0;
    }

    faacEncConfigurationPtr faacConfig = faacEncGetCurrentConfiguration(faac);

    faacConfig->mpegVersion   = MPEG4;
//  faacConfig->aacObjectType = MAIN;
    faacConfig->aacObjectType = LOW;
    faacConfig->allowMidside  = 0;
    faacConfig->useLfe        = 0;
    faacConfig->useTns        = 0;
    faacConfig->bitRate       = 16000; // per channel
//  faacConfig->quantqual     = 100;
    faacConfig->outputFormat  = 0;  // Raw
    faacConfig->inputFormat   = FAAC_INPUT_16BIT;
    faacConfig->bandWidth     = 0;

    if ( !faacEncSetConfiguration(faac, faacConfig) ) {
        return -1;
    }

    FILE* fd = fopen(inputPath, "rb");
    if ( fd == NULL ) {
        return -2;
    }
    FILE* fdout = fopen(outputPath, "wb+");
    if ( fdout == NULL ) {
        return -3;
    }

    char* bufSrc = new char[faacInputSamples*2];    // 每个采样16位PCM,2字节
    char* bufDst = new char[faacMaxOutputBytes];

    while ( 1 ) {
        int read = fread( bufSrc, faacInputSamples, 2, fd );
        if( read < 1 )
            break;
        int nread = faacEncEncode(faac, (int32_t *)bufSrc, (unsigned int)faacInputSamples, (unsigned char*)bufDst, faacMaxOutputBytes);

        fwrite( bufDst, nread, 1, fdout );
    }

    fclose( fdout );
    fclose( fd );
    delete[] bufSrc;
    delete[] bufDst;

    faacEncClose( faac );

    return 1;
}
4

1 回答 1

0

你的程序没有问题。但是您将输出格式配置为 RAW,因此输出文件是无头的。您必须将输出文件配置为 faacConfig->outputFormat = 1; 然后你就可以使用FAAD2来解码文件了!

于 2013-04-25T16:26:49.543 回答