m0_61770108 2024-07-11 14:16 采纳率: 13.5%
浏览 4
已结题

将音频帧AVFrame的格式有AAC转为PCM后,再将AVFrame中的data转为QByteArray 之后,用QIODevice写入,QAudioOutput播放音频,但是播放无音频。

将音频帧AVFrame的格式有AAC转为PCM后,再将AVFrame中的data转为QByteArray 之后,用QIODevice写入,QAudioOutput播放音频,但是播放无音频。
源码:


//初始化AVFrame
    pFrame = av_frame_alloc();
    pFramePCM = av_frame_alloc();

    const AVChannelLayout *out_ch_layout = &out_codec_ctx->ch_layout;
    const AVChannelLayout *in_ch_layout = &pCodecCtx->ch_layout;
    SwrContext *swr_ctx2 = swr_alloc();

    //创建和配置SwrContext上下文,指定转换后的音频格式,包括输入输出采样率、通道布局、样本格式
    swr_alloc_set_opts2(&swr_ctx2,out_ch_layout, AV_SAMPLE_FMT_S16, out_codec_ctx->sample_rate,in_ch_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate,0, NULL);
    swr_init(swr_ctx2);

    packet = av_packet_alloc();

    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == audioStream) {
            if(avcodec_send_packet(pCodecCtx, packet) == 0){
                while (avcodec_receive_frame(pCodecCtx, pFrame) >= 0) {

                    int outNbSamples = swr_get_out_samples(swr_ctx2, pFrame->nb_samples);

                    av_samples_alloc((uint8_t **)&pFramePCM->data, &pFramePCM->linesize[0],pCodecCtx->ch_layout.nb_channels, outNbSamples,AV_SAMPLE_FMT_S16, 1);

                    swr_convert(swr_ctx2, pFramePCM->data, outNbSamples,(const uint8_t **)pFrame->data, pFrame->nb_samples);

                    QByteArray audioData((char *)pFramePCM->data, pFramePCM->nb_samples * pCodecCtx->ch_layout.nb_channels * 3);

                    streamOut->write(audioData.data(),audioData.size());

                    qDebug()<<"接收到了音频";
                }
            }
        }
        av_packet_unref(packet);
    }
  • 写回答

4条回答 默认 最新

  • 百锦再@新空间代码工作室 全栈领域优质创作者 2024-07-11 14:16
    关注

    Gpt辅助回答,我来为你把关

    //以下为您提供的代码
    

    根据您提供的代码,确保以下几点:

    1. 确保在转换AAC到PCM之后,数据存储在pFramePCM->
      data
      中,并且nb_samples为转换后的采样数。
    2. 需要将pFramePCM->
      data
      转换为QByteArray,并且确保转换正确。
    3. 确保在使用QIODevice写入数据时,指定正确的数据大小。
    4. 确保使用QAudioOutput播放音频时,已经正确配置音频输出参数。

    如果以上问题都没有引起播放无音频的情况,建议您检查是否存在其他问题,可以通过打印日志或者调试器进一步排查问题。


    有问题你别着急,评论留言都可以,看到马上就回复,尽量及时补充齐
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月2日
  • 创建了问题 7月11日

悬赏问题

  • ¥15 Opencv(C++)异常
  • ¥15 VScode上配置C语言环境
  • ¥15 汇编语言没有主程序吗?
  • ¥15 这个函数为什么会爆内存
  • ¥15 无法装系统,grub成了顽固拦路虎
  • ¥15 springboot aop 应用启动异常
  • ¥15 matlab有关债券凸性久期的代码
  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错