#pragma once extern "C" { #include #include #include #include } struct sonicStreamStruct; #include #include #include #include class AudioPlayer { public: AudioPlayer(); ~AudioPlayer(); // 初始化音频输出,准备播放 bool open(const AVFrame* frame, float speed = 1.0f); // 播放一帧音频数据 bool play(const AVFrame* frame, float speed = 1.0f); // 设置播放速度 void setSpeed(float speed); // 设置音量(0.0~1.0) void setVolume(float volume); // 切换音频输出设备 bool setOutputDevice(const QAudioDeviceInfo& deviceInfo); // 停止播放 void stop(); // 释放所有资源 void reset(); // 获取当前输出设备 QIODevice* getOutputDevice() const; QAudioOutput* getAudioOutput() const; float getSpeed() const; float getVolume() const; private: void freeBuffers(); bool initAudioOutput(const AVFrame* frame, float speed); bool ensureResampler(const AVFrame* frame); bool ensureSonic(int sampleRate, int channels); void releaseResampler(); void releaseSonic(); void releaseAudioOutput(); int m_sampleRate = 0; int m_channels = 0; AVSampleFormat m_format = AV_SAMPLE_FMT_NONE; SwrContext* m_swrCtx = nullptr; uint8_t* m_swrBuffer = nullptr; int m_swrBufferSize = 0; sonicStreamStruct* m_sonicCtx = nullptr; QAudioOutput* m_audioOutput = nullptr; QIODevice* m_audioDevice = nullptr; QAudioDeviceInfo m_deviceInfo; float m_speed = 1.0f; float m_volume = 1.0f; uint8_t* m_abufOut = nullptr; unsigned int m_abufOutSize = 0; QMutex m_mutex; };