void
record_audio(){
int
ret = 0;
int
num = 0;
char
errors[1024];
AVFormatContext *fmt_ctx = NULL;
AVDictionary *options = NULL;
int
count = 0;
AVPacket pkt;
char
*devicename =
":0"
;
av_log_set_level(AV_LOG_DEBUG);
avdevice_register_all();
AVInputFormat *iformat = av_find_input_format(
"avfoundation"
);
if
( (ret = avformat_open_input( &fmt_ctx, devicename, iformat, &options)) < 0){
av_strerror(ret, errors, 1024);
printf
(stderr,
"failed to open audio device, [%d] %s\n"
, ret,errors);
}
av_init_packet(&pkt);
usleep (30000);
char
*out =
"/Users/liuyong/audio/audio.pcm"
;
FILE
*outfile =
fopen
(out,
"wb+"
);
while
((num = av_read_frame(fmt_ctx, &pkt)) == 0 && count++ < 500) {
fwrite
(pkt.data, pkt.size, 1, outfile);
fflush
(outfile);
av_log(NULL, AV_LOG_INFO,
"pkt size is %d(%p),count=%d \n"
, pkt.size, pkt.data, count);
usleep (30000);
av_packet_unref(&pkt);
}
fclose
(outfile);
avformat_close_input(&fmt_ctx);
av_log(NULL, AV_LOG_DEBUG,
"finish!\n"
);
return
;
}