jint setData(JNIEnv *env,jclass clz,jstring name){ pause=1; i=0; tni=0; LOGI("%s","setData"); const char *file=(*env)->GetStringUTFChars(env,name,0); if(avformat_open_input(&pFormatCtx,file,NULL,NULL)!=0)return -1; if(avformat_find_stream_info(pFormatCtx,NULL)<0)return -2; av_dump_format(pFormatCtx,-1,file,0); audioIndex=-1; int index; audioIndex=av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &pCodec, 0); if(audioIndex<0)return -3; pCodecCtx = pFormatCtx->streams[audioIndex]->codec; av_log(NULL,AV_LOG_INFO,"解码器:%s",pCodec->name); ran= pFormatCtx->streams[audioIndex]->time_base; duration = pFormatCtx->streams[audioIndex]->duration; if(avcodec_open2(pCodecCtx,pCodec,NULL)<0)return -4; pFrame=av_frame_alloc(); if(pFrame==NULL)return -5; packet = (AVPacket *)av_malloc(sizeof(AVPacket)); av_init_packet(packet); ret=av_read_frame(pFormatCtx,packet); init_swr(); i=1; (*env)->ReleaseStringUTFChars(env,name,file); return 0; }
/** * Initialize our main context FFAudioIO, so that SwrContext, decode buffers and the encoder are set * to reasonable values. * * @param JNIEnv env * @param aio our context, FFAudioIO * @return a negative value, if something went wrong */ int ff_init_audioio(JNIEnv *env, FFAudioIO *aio) { int res = 0; int nb_planes; AVCodec *codec = NULL; aio->timestamp = 0; // allocate pointer to the audio buffers, i.e. the multiple planes/channels. nb_planes = av_sample_fmt_is_planar(aio->stream->codecpar->format) ? aio->stream->codecpar->channels : 1; // always init SWR to keep code simpler res = init_swr(env, aio); if (res < 0) { // exception is already thrown goto bail; } // if for some reason the codec delivers 24bit, we need to encode its output to little endian if (aio->stream->codecpar->bits_per_coded_sample == 24) { codec = ff_find_encoder(aio->stream->codecpar->format, aio->stream->codecpar->bits_per_coded_sample, ff_big_endian(aio->stream->codecpar->codec_id), 1); if (!codec) { res = AVERROR(EINVAL); throwIOExceptionIfError(env, res, "Could not find suitable encoder codec."); goto bail; } res = ff_init_encoder(env, aio, codec); if (res<0) { throwIOExceptionIfError(env, res, "Could not initialize encoder codec."); goto bail; } } // allocate the buffer the codec decodes to aio->audio_data = av_mallocz(sizeof(uint8_t *) * nb_planes); if (!aio->audio_data) { res = AVERROR(ENOMEM); throwIOExceptionIfError(env, res, "Could not allocate audio data buffers."); goto bail; } aio->decode_frame = av_frame_alloc(); if (!aio->decode_frame) { res = AVERROR(ENOMEM); throwIOExceptionIfError(env, res, "Could not allocate frame."); goto bail; } // initialize packet av_init_packet(&(aio->decode_packet)); aio->decode_packet.data = NULL; aio->decode_packet.size = 0; bail: return res; }