我在哪里可以找到该文件和Android写入JNI

时间:2015-11-11 02:23:01

标签: android c ffmpeg java-native-interface fopen

我为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 ;

      }

1 个答案:

答案 0 :(得分:0)

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