播放录制的pcm没有声音
来源:6-11 录制音频数据

慕仰8519611
2022-03-03
刚开始的时候av_read_frame一直返回-35,所以while内外添加了sleep(1),录制之后,根据群公告查询了ffplay参数之后使用ffplay -ar 44100 -ac 2 -f f32le ./record.pcm 播放,发现Duration: 00:00:00.05,特别的短。感觉导致这一系列问题的原因是因为这个sleep阻塞了线程,但是不加又返回-35,导致进不了循环。所以这问题怎么解决啊。代码啥的检查了几次 也没见啥问题。fflay的参数也根据自己机器的情况设定的
写回答
4回答
-
星火流云
2022-05-14
请问这个问题解决了吗?我将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; }
012022-05-17 -
星火流云
2022-05-14
我也出现了这个问题,请问解决了吗
00 -
想不到待定
2022-04-05
同学 你这个问题解决了吗
00 -
李超
2022-04-02
进入课程QQ群,看一下公告
00
相似问题