Qt播放amr文件

探索历程

  1. 使用QMediaPlayer播放,失败。 使用QAudioOutput播放,各种调参数,失败。
  2. 询问ChatGPT和NewBing和各种谷歌,最终结论是Qt无法直接播放amr,需要自己写解码器,然后用QMediaPlayer等播放,原地起飞的冲动。
  3. 根据GPT提示,开始研究opencore-amr三方,下载源码各种尝试,要自己make打包静态库。
  4. 折腾最后总算是将opencore-amr接入项目调用方法没有报错,继续查资料。
  5. 网上资料无法找到,直接在github中全局搜索相关代码进行筛选研究。
  6. github上没有找到Qt直接可以用的示例代码,继续网上研究和GPT与Bing轮番询问。
  7. 确认opencore-amr将amr转化为pcm进行播放方案,类比github上代码开始组装自己代码。
  8. amr到pcm代码处理完毕,不知道对不对,开始测试,声音无法正常播放,有声音却播放速度非常快,但不同amr文件音调不同,怀疑是逐帧播放问题。
  9. 继续github全局搜索,确认amr到wav方案,找到相关相似代码示例,模仿测试。
  10. 测试失败,Qt中fopen貌似有问题,开始网上查找,找到原因,最终播放成功。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
#include <opencore-amrnb/interf_dec.h>
#include <stdio.h>
#include "opencore/wav.h"

const int sizes[] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 6, 5, 5, 0, 0, 0, 0 };

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    player = new QMediaPlayer();
}

MainWindow::~MainWindow()
{
    delete ui;

}

// 播放按钮点击
void MainWindow::on_play_clicked()
{

    QTextCodec *code = QTextCodec::codecForName("GB2312");
    std::string name = code->fromUnicode("E:/AmrTest/input.amr").data();

    // name需要处理一下
    FILE* in = fopen(name.c_str(), "rb");
    if (!in) {
        return;
    }
    char header[6];
    size_t n = fread(header, 1, 6, in);
    if (n != 6 || memcmp(header, "#!AMR\n", 6)) {
        fprintf(stderr, "Bad header\n");
        return;
    }

    // 本地文件路径
    QString fileName = "E:/AmrTest/output.wav";
    // 将wav放进了本地文件
    WavWriter wav(std::string(fileName.toLocal8Bit()).c_str(),8000,16,1);

    void* amr = Decoder_Interface_init();
    while (1) {
        uint8_t buffer[500];
        /* Read the mode byte */
        n = fread(buffer, 1, 1, in);
        if (n <= 0)
            break;
        /* Find the packet size */
        int size = sizes[(buffer[0] >> 3) & 0x0f];
        if (size <= 0)
            continue;
        n = fread(buffer + 1, 1, size, in);
        if (n != size)
            break;
        /* Decode the packet */
        int16_t outbuffer[160];
        Decoder_Interface_Decode(amr, buffer, outbuffer, 0);
        /* Convert to little endian and write to wav */
        uint8_t littleendian[320];
        uint8_t* ptr = littleendian;
        for (int i = 0; i < 160; i++) {
            *ptr++ = (outbuffer[i] >> 0) & 0xff;
            *ptr++ = (outbuffer[i] >> 8) & 0xff;
        }

        wav.writeData(littleendian, 320);
    }
    fclose(in);
    Decoder_Interface_exit(amr);

    player->setMedia(QUrl(fileName));
    player->play();
}

代码位置:https://github.com/anchoriteFili/AmrTest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值