群公告里并没有看到关于录制无声音适合我的解决方案
这是我通过ffmpeg命令采集音频查看到的音频参数
Audio: pcm_s16le ([1][0][0][0] / 0x0001), 48000 Hz, 1 channels, s16, 768 kb/s
然后我播放的时候没有声音,个人感觉音频没声音跟这三个参数应该无关吧,最多就是解码出来声音杂乱无章,也不至于无声,应该还是录制上出了问题。
#include "testc.h"
#include <unistd.h>
void rec_audio(){
int ret = 0;
char errors[1024];
AVFormatContext *fmt_ctx = NULL;
AVDictionary *options = NULL;
char* device_name = ":0";
AVPacket pkt;
int count = 0;
//注册音频硬件
avdevice_register_all();
//获取文件类型
AVInputFormat *iformat = av_find_input_format("avfoundation");
//打开设备
ret = avformat_open_input(&fmt_ctx, device_name, iformat, &options);
if(ret < 0){
av_strerror(ret, errors, 1024);
printf(stderr,"Failed to open Audio Device,[%d]%s\n",ret,errors);
return;
}
printf("Open Device Succes\n");
av_init_packet(&pkt);
char *out ="/Users/lihaochen/Downloads/audio.pcm";
FILE *outfile = fopen(out,"wb+");
while(count < 500){
//从设备读取数据
ret = av_read_frame(fmt_ctx, &pkt);
if(ret == -35){
av_packet_unref(&pkt);
printf("device not ready");
sleep(1);
continue;
}
if(ret == 0){
printf("packet size is %d \n",pkt.size);
//写入文件
fwrite(pkt.data, pkt.size, 1, outfile);
fflush(outfile);
//释放pkt
av_packet_unref(&pkt);
usleep(30 * 1000);
}else{
av_packet_unref(&pkt);
break;
}
count++;
}
if(ret < 0){
av_strerror(ret, errors, 1024);
printf("Failed to open Audio Device,[%d]%s\n",ret,errors);
}
//关闭文件
fclose(outfile);
//关闭设备,释放上下文
avformat_close_input(&fmt_ctx);
printf("finish");
}