代码之家  ›  专栏  ›  技术社区  ›  Amr Ayman

用C语言播放mp3文件

  •  1
  • Amr Ayman  · 技术社区  · 10 年前

    我想在互联网上播放mp3文件而不下载它们。因此,我使用libcurl将其作为内存中的流获取,如下所示:

    static size_t use_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
         /* stream is NULL */
         /* What to do with the stream of data ? */
    }
    
    CURLcode download_file(const char *url, const char *path, curl_progress_callback progress) {
        CURL *curl;
        CURLcode res = 0;
        FILE *fp;
        if ((curl = curl_easy_init())) {
            if (progress) {
                curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
                curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
            }
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, use_data);
            res = curl_easy_perform(curl);
            /* always cleanup */
            curl_easy_cleanup(curl);
            fclose(fp);
        }
        return res;
    }
    

    如何解析内存中的流以播放声音?

    1 回复  |  直到 10 年前
        1
  •  2
  •   Sergey K.    9 年前

    IMHO最简单的方法是使用轻量级MP3解码库。例如,minimp3完成了它的任务,并且只包含2个文件。

    http://keyj.emphy.de/minimp3

    API非常简单,可以在这里找到使用示例: https://github.com/corporateshark/PortAMP/tree/master/src/Decoders/MP3