ffmpeg播放器19

173 阅读2分钟

1背景

前面分析了HFrameBuf,从dotask中可以看到只存储了视频帧。播放器15里提到了timer是用来控制音视频同步的。这一篇处理音频。

2步骤

2.1qt播放音频(不可行)

OpenGL方面不用考虑了,那个只有图像渲染。使用ds可以搜到qt有下面两种。

  • 使用QMediaPlayer播放背景音乐
  • 使用QSoundEffect播放音效
QMediaPlayer *backgroundMusic = new QMediaPlayer;
backgroundMusic->setSource(QUrl::fromLocalFile(":/sounds/background.mp3"));
backgroundMusic->setLoops(QMediaPlayer::Infinite);  // 循环播放
backgroundMusic->play();



QSoundEffect *effect = new QSoundEffect;
effect->setSource(QUrl::fromLocalFile(":/sounds/click.wav"));
effect->setVolume(0.5f);  // 设置音量(0.0到1.0之间)

然后查了一下跨平台支持的格式 未压缩音频: WAV (PCM), AIFF (PCM) 压缩音频: OGG Vorbis(需 Linux 安装插件)

  1. Windows 依赖框架: Windows Media Foundation (WMF) 或 DirectShow。 常见支持格式: MP3, WAV, WMA, AAC AIFF, FLAC(需系统安装解码器) MIDI(部分系统支持)

  2. Linux 依赖框架: GStreamer(需安装  gstreamer  和插件)。 常见支持格式: MP3, OGG Vorbis, WAV, FLAC, AAC ALAC, Opus(需安装  gst-plugins-good  和  gst-plugins-bad )

  3. macOS/iOS 依赖框架: AVFoundation。 常见支持格式: MP3, WAV, AAC, ALAC, FLAC AIFF, CAF(Core Audio Format)

  4. Android 依赖框架: Android MediaPlayer。 常见支持格式: MP3, WAV, AAC, 3GPP OGG(需 Android 3.0+) FLAC(需 Android 3.1+)

2.2 QAudioOutput(尝试1)

ffmpeg解码后的音频pcm可以使用这个QAudioOutput来播放

// 示例:通过 FFmpeg 解码后播放
QAudioFormat format;
format.setSampleRate(44100);
format.setChannelCount(2);
format.setSampleFormat(QAudioFormat::Int16);

QAudioOutput *audioOutput = new QAudioOutput(format);
audioOutput->start();

// 将 FFmpeg 解码后的 PCM 数据写入设备
QIODevice *audioDevice = audioOutput->start();
audioDevice->write(pcmData);

doc.qt.io/archives/qt…

Detailed Description An audio format specifies how data in a raw audio stream is arranged. For example, how the stream is to be interpreted.

QAudioFormat contains parameters that specify how the audio sample data is arranged. These are the frequency, the number of channels, and the sample format. The following table describes these in more detail.

Parameter Description

  • Sample Rate Samples per second of audio data in Hertz.
  • Number of channels The number of audio channels (typically one for mono or two for stereo). These are the amount of consecutive samples that together form one frame in the stream
  • Sample format The format of the audio samples in the stream

2.3 OpenAL(最后再尝试)

高级选项:使用OpenAL实现3D音效 如需更复杂的音频处理(如3D空间音效),可集成OpenAL库: 下载OpenAL库(如OpenAL Soft)。 在.pro中添加链接: LIBS += -lopenal 。 使用OpenAL API控制声音的位置和衰减。

2.4 换sdl(尝试2)

ffmpeg.org/doxygen/tru…

ffmpeg的player源码中有如下

     flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
     if (audio_disable)
         flags &= ~SDL_INIT_AUDIO;
     else {
         /* Try to work around an occasional ALSA buffer underflow issue when the
          * period size is NPOT due to ALSA resampling by forcing the buffer size. */
         if (!SDL_getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE"))
             SDL_setenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE","1", 1);
     }
     if (display_disable)
         flags &= ~SDL_INIT_VIDEO;
     if (SDL_Init (flags)) {
         av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
         av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
         exit(1);
     }

sdl支持音频播放