在ffmpeg(1)的基础上增加代码
bool openaudio()
{
char *name = "audio=麦克风 (Realtek High Definition Audio)";
int ret = 0;
AVFormatContext *avformatcontext = NULL;
AVDictionary *dt = NULL;
int count = 0;
AVPacket pkt;
avdevice_register_all();
AVInputFormat *avinputformat = av_find_input_format("dshow");
// int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
ret = avformat_open_input(&avformatcontext,name,avinputformat,&dt);
if(ret != 0)
{
printf("open device is failed\n");
return false;
}
printf("open device is ok\n");
char *filename = "audio.pcm";
FILE *fp = fopen(filename,"wb+");
if(fp == 0)
{
printf("fopen(%s) is failed\n",filename);
return false;
}
printf("fopen(%s) is ok\n",filename);
while(count++ < 50)
{
ret = av_read_frame(avformatcontext,&pkt);
if(ret != 0)
{
printf("av_read_frame() is failed\n"); break;
}
printf("pkt.size=%d,pkt.data=%d,count=%d\n",pkt.size,pkt.data,count);
fwrite(&pkt.data,1,pkt.size,fp);
fflush(fp);
}
fclose(fp);
return true;
}