【问题标题】:Play mp3 file in C在 C 中播放 mp3 文件
【发布时间】:2015-10-28 07:32:44
【问题描述】:

我想在互联网上播放 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;
}

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

【问题讨论】:

  • 您的问题有很多可能的答案。
  • @iharob:好吧,看来,我没有得到任何答案。
  • 是的,因为您必须提出一个具体问题。你的问题很模棱两可。您是否搜索过可以帮助播放 mp3 的库?
  • @iharob:是的,很多。但我想要的比从文件中读取更具体。直接从内存中逐行读取。
  • 我认为ffmpeglibavcodec 可以从内存缓冲区中读取,我只是不记得具体如何,搜索它的文档。

标签: c mp3 libcurl audio-player


【解决方案1】:

恕我直言,最简单的方法是使用轻量级 MP3 解码库。例如,minimp3 完成了它的工作,并且只包含 2 个文件。

http://keyj.emphy.de/minimp3

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

【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多