我在哪里可以找到文件并 android 在 JNI 中写入

where can i find the file and android write in JNI

我为 ffmpeg 编写了一个测试演示,读取了一个 flac 文件并将其解码为一个新的 pcm 文件。

完成工作后,我找不到输出文件。

所以我用一个新的FILE指针打开输出文件,打开就可以了,有没有人给点提示,谢谢。

static int decode_packet(int *got_frame, int cached)
{
    int ret = 0;
    int decoded = pkt.size;

    *got_frame = 0;

    if (pkt.stream_index == audio_stream_idx) {
        /* decode audio frame */
        ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
        if (ret < 0) {
            LOG("Error decoding audio frame (%s)\n", av_err2str(ret));
            return ret;
        }

        decoded = FFMIN(ret, pkt.size);

        if (*got_frame) {
            LOG("frame->format: %d", frame->format);
            size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16/*frame->format*/);
            LOG("audio_frame%s n:%d nb_samples:%d pts:%s\n", cached ? "(cached)" : "", audio_frame_count++, frame->nb_samples,
                   av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));

            fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
        }
    }

    if (*got_frame && api_mode == API_MODE_NEW_API_REF_COUNT)
        av_frame_unref(frame);

    return decoded;
}

    // as convient i write the abspath `/storage/emulated/0` which i got by rountine Environment.getExternalStorageDirectory().getAbsolutePath()
    const char *src_filename = "/storage/emulated/0/pyghlkn.flac";
    const char *audio_dst_filename = "/storage/emulated/0/test.pcm";

    FILE *audio_dst_file = NULL;
    FILE *audio_dump = NULL;

    JNIEXPORT void JNICALL Java_cn_com_longmaster_ffmpeg_encoder
      (JNIEnv *, jobject)
      {
          //......
            /* read frames from the file */
            while (av_read_frame(fmt_ctx, &pkt) >= 0) {
                AVPacket orig_pkt = pkt;
                do {
                    ret = decode_packet(&got_frame, 0);
                    LOG("write decode_packet... pkt.size: %d ", pkt.size);
                    if (ret < 0)
                        break;
                    pkt.data += ret;
                    pkt.size -= ret;
                } while (pkt.size > 0);
                av_free_packet(&orig_pkt);
            }

            /* flush cached frames */
            pkt.data = NULL;
            pkt.size = 0;
            do {
                decode_packet(&got_frame, 1);
                LOG("flush cached frames");
            } while (got_frame);


            if (audio_dump)
            {
                LOG("default audio_dump ready...");
            }
            else
            {
                LOG("default audio_dump not ready...");
            }

            audio_dump = fopen(audio_dst_filename, "rb");
            if (audio_dump) {
                LOG("open pcm file ok...");
            } else {
                LOG("open pcm file fail...");
            }

            avcodec_close(audio_dec_ctx);
            avformat_close_input(&fmt_ctx);
            if (audio_dst_file) {
                fclose(audio_dst_file);
            }

            av_frame_free(&frame);

            return ;

      }

您应该在应用程序的 java 部分使用 File dirBase = Environment.getExternalStorageDirectory(); // Root directory of SD card 而不是硬编码路径,并将其传递给您的 C 程序。 这条路径可以是例如/storage/sdcard 取决于您的 andorid 供应商 phone。使用该功能将始终为您提供正确的路径。