请问这个问题解决了吗?我将sleep换成usleep(30000) 30ms是可录音的,但是使用下面命令进行音的时候,速度特别快,15s的录音基本上5s就完了,不知道为什么
ffplay -ar 44100 -ac 1 -f f32le audio.pcm
void record_audio(){
int ret = 0;
int num = 0;
char errors[1024];
// ctx
AVFormatContext *fmt_ctx = NULL;
AVDictionary *options = NULL;
// pakcet
int count = 0;
AVPacket pkt;
// device
char *devicename = ":0";
// set log level
av_log_set_level(AV_LOG_DEBUG);
// register audio device
avdevice_register_all();
// get format
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+");
// read data from device
while ((num = av_read_frame(fmt_ctx, &pkt)) == 0 && count++ < 500) {
// write file
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);
}
// close file
fclose(outfile);
//close device and release ctx
avformat_close_input(&fmt_ctx);
av_log(NULL, AV_LOG_DEBUG,"finish!\n");
return;
}