Demo程序代码
#include "audio_ffmpegc.h"
void rec_audio(){
int ret = 0;
char errors[1024] = {0,};
AVFormatContext *fmt_ctx = NULL;
const AVInputFormat *iformat = NULL;
AVDictionary *options = NULL;
//[[video device]:[audio device]]
const char *devicename = ":0";
av_log_set_level(AV_LOG_DEBUG);
//register audio device
avdevice_register_all();
/**
* get format
* macos: avfoundation; windowos:dshow; linux: alsa;
* ffmpeg -list_devices true -f avfoundation -i dummy
**/
iformat = av_find_input_format("avfoundation");
if(iformat == NULL){
av_log(NULL, AV_LOG_ERROR, "avformat is null \n");
}
ret = avformat_open_input(&fmt_ctx, devicename, iformat, &options);
if(ret < 0){
av_strerror(ret, errors, 1024);
av_log(NULL,AV_LOG_ERROR, "Failed to open audio device, [%d] %s\n", ret, errors);
return;
}
int count = 0;
AVPacket *pkt;
pkt = av_packet_alloc();
while (ret = av_read_frame(fmt_ctx, pkt) && (count++ < 10)== 0) {
printf("pkt size is [%d]", pkt->size);
}
av_packet_free(&pkt);
avformat_close_input(&fmt_ctx);
return;
}
void set_status(int status){
printf("this is in set_status ... ... \n");
return;
}
Debug运行报:
主要是iOS10以后需要说明使用的权限,解决方案如下:
a) 第一步:项目名称右键创建文件并命名为Info.plist
b)第二步:打开Info文件,点击Information Property List 后的 '+'号,添加Microphone Usage Description key并设置Value为YES;(操作图如下)
添加完成,再次Debug正确