• 跨平台音频播放库


    一、ffmpeg及ffplay

    ffmpeg介绍

    1. Fmpeg 是领先的多媒体框架,能够解码、编码、转码、混合、解密、流媒体、过滤和播放人类和机器创造的几乎所有东西。它支持最晦涩的古老格式,直到最尖端的格式。无论它们是由某个标准委员会、社区还是公司设计的。它还具有高度的便携性。
    2. FFmpeg 可以在 Linux、Mac OS X、Microsoft Windows、BSDs、Solaris 等各种构建环境、机器架构和配置下编译、运行,并通过测试基础设施 FATE。
    3. 它包含了 libavcodec、libavutil、libavformat、libavfilter、libavdevice、libswscale 和 libswresample,可以被应用程序使用。还有 ffmpeg、ffplay 和 ffprobe,可以被终端用户用于转码和播放。

    ffplay介绍

       ffplay为ffmpeg中的一部分,官网给的源码编译后默认无此模块,需要提前安装sdl库,安装完成后才可编译出ffplay模块。执行简单命令ffplay alarm.mp3即可播放。
    

    测试结果

    1. Windows:正常播放;
    2. 中标 x86:需要源码编译ffmpeg及相关库,且在中标下编译完成后运行失败,未完成验证。

    相关链接

      [ffmpeg在centos7下编译安装无ffplay的问题解决_YellowShite的博客-CSDN博客](//blog.csdn.net/YellowShite/article/details/112305636?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242.0>)
    

    调研结论

      该方案可行,但存在环境问题,需要在linux客户机进行编译ffmpeg及ffplay模块。且仅使用ffplay为ffmpeg中的一部分,其成本较大。
    

    二、miniaudio

    miniaudio介绍

    miniaudio是一个只有单.h文件的音频播放和抓取库。它跨平台,它简单易用。相比PortAudio和rtAudio,它只有一个.h文件,直接引用,不需要提前编译成lib文件。

     GitHub - mackron/miniaudio: Single file audio playback and capture library written in C.

    star 1.8k!!!!

    播放MP3

    1. #define MINIAUDIO_IMPLEMENTATION
    2. #include "miniaudio.h"
    3. #include
    4. void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
    5. {
    6. ma_decoder* pDecoder = (ma_decoder*)pDevice->pUserData;
    7. if (pDecoder == NULL) {
    8. return;
    9. }
    10. ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount);
    11. (void)pInput;
    12. }
    13. int main()
    14. {
    15. ma_result result;
    16. ma_decoder decoder;
    17. ma_device_config deviceConfig;
    18. ma_device device;
    19. const char* pFilePath = "D:/alarm.mp3";
    20. // const char* pFilePath = "./alarm.mp3";
    21. result = ma_decoder_init_file(pFilePath, NULL, &decoder);
    22. if (result != MA_SUCCESS) {
    23. return -2;
    24. }
    25. deviceConfig = ma_device_config_init(ma_device_type_playback);
    26. deviceConfig.playback.format = decoder.outputFormat;
    27. deviceConfig.playback.channels = decoder.outputChannels;
    28. deviceConfig.sampleRate = decoder.outputSampleRate;
    29. deviceConfig.dataCallback = data_callback;
    30. deviceConfig.pUserData = &decoder;
    31. if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
    32. printf("Failed to open playback device.\\n");
    33. ma_decoder_uninit(&decoder);
    34. return -3;
    35. }
    36. if (ma_device_start(&device) != MA_SUCCESS) {
    37. printf("Failed to start playback device.\\n");
    38. ma_device_uninit(&device);
    39. ma_decoder_uninit(&decoder);
    40. return -4;
    41. }
    42. printf("Press Enter to quit...");
    43. getchar();
    44. ma_device_uninit(&device);
    45. ma_decoder_uninit(&decoder);
    46. return 0;
    47. }

    其中引用的miniaudio头文件见附件。

    运行程序

    1. Windows
    2. ------------
    3. The Windows build should compile cleanly on all popular compilers without the need to configure any include paths nor link to any libraries.
    4. Linux
    5. ----------
    6. The Linux build only requires linking to `-ldl`, `-lpthread` and `-lm`. You do not need any development packages.

    windows上直接编译。linux下需要依赖-ldl, -lpthread and -lm,即:

    gcc main.c -o out -ldl -lpthread -lm
    

    调研结果

    1.windows中播放正常

    2.ubuntu上播放正常(x86)

    3.树莓派中播放正常(arm32v7)

    4.中标麒麟v7服务版播放正常(x86)

    5.银河麒麟v10服务版播放正常(arm)

    6.centos7播放正常(x86)

    问题

    1.按指定次数播放暂未找到方法。

    2.每次新来语音,正在播放的语音将会停止。

    demo下载地址:(qt编译的小demo)

    miniaudio播放mp3的demo-C++文档类资源-CSDN下载

  • 相关阅读:
    MySQL小记——DDL、DML、DQL
    JavaScript 45 JavaScript 类型转换
    angular记录
    详细图解 Netty Reactor 启动全流程 | 万字长文 | 多图预警
    (Matlab实现)蚂蚁狮子优化算法在电力系统中的应用
    【性能测试】Jmeter —— jmeter计数器
    卷?中学生开始学习人工智能和大模型,附课件!
    用HTML+CSS做一个漂亮简单的个人网页——樱木花道篮球3个页面 学生个人网页设计作品 学生个人网页模板 简单个人主页
    vue简单下载
    一幅长文细学CSS3
  • 原文地址:https://blog.csdn.net/qianlixiaomage/article/details/125890790