SDL音频播放
gcc -o tutorial03 tutorial03.c -lavutil -lavformat -lavcodec -lz -lm \
`sdl-config --cflags --libs`
AUDIO名词解释:
samples:采样,通过PCM来采样,通常样本16bit,PCM的采样精度从14-bit发展到16-bit、18-bit、20-bit直到24-bit
Samples rate:采样率,22.05KHz and 44.1KHz,每秒从连续信号中提取并组成离散信号的采样个数
位速:采样率*样本bit*通道数,CD上未经压缩的音频位速是1411.2 kbit/s(16 位/采样点 × 44100 采样点/秒 × 2 通道)
pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO , 在for循环找找到audiostream的索引i
AVCodecContext *aCodecCtx=pFormatCtx->streams[audioStream]->codec; 得到音频编码的信息
SDL_AudioSpec wanted_spec,spec;
wanted_spec.freq = aCodecCtx->sample_rate; //采样率
wanted_spec.format = AUDIO_S16SYS;
//告诉SDL使用什么格式,S指代signed,16为样本16bit,SYS指代大小端由系统决定
wanted_spec.channels = aCodecCtx->channels; //有多少个通道
wanted_spec.silence = 0; //silence值,由于为signed,故为0
wanted_spec.samples =1024; //缓存大小
wanted_spec.callback = audio_callback; //音频的回调函数
wanted_spec.userdata = aCodecCtx; //给回调函数处理的数据
SDL_OpenAudio(&wanted_spec, &spec)
返回-1则打开失败,spec为NULL则以wanted_spec指定的方式播放,若spec不为NULL,则使用根据硬件改变的spec指定的方式播放,而wanted_spec可以删除
VCodec *aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
avcodec_open(aCodecCtx, aCodec);
找到解码器,并进行解码
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets; //为包的总数
int size; //为所有包的大小
SDL_mutex *mutex; //互斥锁
SDL_cond *cond; //条件变量
} PacketQueue;
我们自己创建的用于构建Packet队列的数据结构
AVPacketList
A simple linked list for packets.
AVPacket pkt
AVPacketList * next
void packet_queue_init(PacketQueue *q)
{
memset(q,0,sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond=SDL_CreateCond();
}
对PacketQueue数据结构进行初始化
用于给PacketQueue数据结构中填入包的函数
int packet_queue_put(PacketQueue *q,AVPacket *pkt)
{
AVPacketList *pkt1;
if(av_dup_packet(pkt)<0)
{
return -1; //检查是否为NULL,为NULL则自己填充,否则返回-1
}
pkt1 = av_malloc(sizeof(AVPacketList));//给AVPacketList分配空间
if (!pkt1) 接收数据 void SDL_PauseAudio(int pause_on) 播放的回调函数,格式必须为void callback(void *userdata, Uint8 *stream, int len),这里的userdata就是我们给到SDL的指针,stream是我们要把声音数据写入的缓冲区指针,len是缓冲区的大小。 struct mydata *data=(struct mydata*)userdata; while(len > 0) { 对音频数据进行解码,被解码的数据存在audio_buf中,buf_size告诉函数audio_buf缓冲区多大,返回值为解码的数据数量,结束时返回-1,否则返回被解码的bytes数。 static AVPacket pkt; //对数据进行解码 int avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples, int *frame_size_ptr, uint8_t *buf, int buf_size) |
SDL音频播放的更多相关文章
- FFMPEG + SDL音频播放分析
目录 [hide] 1 抽象流程: 2 关键实现: 2.1 main()函数 2.2 decode_thread()读取文件信息和音频包 2.3 stream_component_open():设置音 ...
- 最简单的基于FFMPEG+SDL的音频播放器 ver2 (采用SDL2.0)
===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...
- 最简单的基于FFMPEG+SDL的音频播放器 ver2 (採用SDL2.0)
===================================================== 最简单的基于FFmpeg的音频播放器系列文章列表: <最简单的基于FFMPEG+SDL ...
- 最简单的视音频播放示例9:SDL2播放PCM
本文记录SDL播放音频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...
- 最简单的视音频播放示例7:SDL2播放RGB/YUV
本文记录SDL播放视频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...
- ffmpeg音频播放代码示例-avcodec_decode_audio4
一.概述 最近在学习ffmpeg解码的内容,参考了官方的教程http://dranger.com/ffmpeg/tutorial03.html,结果发现这个音频解码的教程有点问题.参考了各种博客,并同 ...
- FFmpeg简易播放器的实现-音频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- 最简单的视音频播放演示样例7:SDL2播放RGB/YUV
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
- 视频和音频播放的演示最简单的例子9:SDL2广播PCM
===================================================== 最简单的视频和音频播放的演示样品系列列表: 最简单的视音频播放演示样例1:总述 最简单的视音 ...
随机推荐
- Apache Tomcat8必备知识
Apache Tomcat8必备知识 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs 一.Apache Tomcat 8介绍 Apache Tomcat ...
- IE的CSS相关的BUG(整理一)
本来不想弄这个ie的bug的,真的很想让它快点死掉,可是事与愿违啊,没办法,还是贴出来,以备自用. 这个网页(http://haslayout.net/css/index)上例举了所有的IE和CSS相 ...
- 高性能 Socket 组件 HP-Socket v3.2.1-RC5 公布
HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C+ ...
- Example of how to use both JDK 7 and JDK 8 in one build.--reference
JDK 8 Released Most of us won’t be able to use/deploy JDK 8 in production for a looong time. But tha ...
- Android TagFlowLayout完全解析 一款针对Tag的布局(转)
一.概述 本文之前,先提一下关于上篇博文的100多万访问量请无视,博文被刷,我也很郁闷,本来想把那个文章放到草稿箱,结果放不进去,还把日期弄更新了,实属无奈. ok,开始今天的博文,今天要说的是Tag ...
- Java基础知识强化83:System类之gc()方法(垃圾回收)以及和finalize()区别
1. System概述: System类包含一些有用的类字段和方法.它不能被实例化. 2. gc()方法:垃圾回收器 public static void gc() 调用gc方法暗示着Ja ...
- 洛谷 P1412 经营与开发
/* 粘一下开始写的暴力吧 虽然没啥价值 */ #include<iostream> #include<cstdio> #include<cstring> #inc ...
- setTimeout()与setInterval()方法区别介绍
计时器setTimeout()和setInterval()两个都是js的计时功能的函数两个有些区别,下面为大家简单介绍下,希望对大家有所帮助 计时器setTimeout()和setInterval ...
- PHP常用代码大全
1.连接MYSQL数据库代码 <?php $connec=mysql_connect("localhost","root","root" ...
- Mysql锁机制介绍
Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...