在前面文中已经提到了Qt+ffmpeg的环境配置和视频播放
下面是连接:
环境配置:
https://blog.youkuaiyun.com/birdlearn/article/details/116718558
视频播放:
https://blog.youkuaiyun.com/birdlearn/article/details/116752261
如果有兴趣的同学看了前面两节的内容应该大概就对ffmpeg有了一定的了解
本章就基于Qt的Ffmpeg做的一个音频播放,代码仍然使用的是雷神代码。
直接贴出代码
#include <QCoreApplication>
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
//Output PCM
#define OUTPUT_PCM 1
//Use SDL
#define USE_SDL 1
extern "C"
{
#include "SDL2/SDL.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
}
void fill_audio(void *udata,Uint8 *stream,int len);
//chunk-------pos---len-----|
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;
void fill_audio(void *udata,Uint8 *stream,int len){
//SDL 2.0
SDL_memset(stream, 0, len);
if(audio_len==0) /* Only play if we have data left */
return;
len=(len>audio_len?audio_len:len); /* Mix as much data as possible */
SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
audio_pos += len;
audio_len -= len;
}
#undef main
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
AVFormatContext *pFormatCtx;
int i, audioStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVPacket *packet;
uint8_t *out_buffer;
AVFrame *pFrame;
SDL_AudioSpec wanted_spec;
int ret;
uint32_t<