带有 Qt 的 mpg123 库

发布于 2024-10-09 05:11:58 字数 471 浏览 0 评论 0原文

我想在我的嵌入式 Linux Qt 应用程序中添加对 mp3 文件播放的支持。

我无法在 Qt 中使用声子。在 .pro 文件中添加 QT += phonon 后,在编译过程中出现以下错误: /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libphonon.so:对 `QWidget::x11Event(_XEvent*)'

/usr 的 未定义引用/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libphonon.so:对“QDataStream::QDataStream(QByteArray*, int)”的未定义引用

collect2: ld 返回 1 退出状态

所以现在我正在考虑使用 mpg123 lib 来解码 mp3 文件。

我需要帮助将库集成到 Qt 中。我以前从未在 Qt 中使用过纯 C++ 库,所以我不太了解如何集成它。

I want add support for playback of mp3 file in my Qt app for embedded linux.

I'm not able to use phonon in Qt. After adding QT += phonon in .pro file it gives me the following error during compilation :
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libphonon.so: undefined reference to `QWidget::x11Event(_XEvent*)'

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libphonon.so: undefined reference to `QDataStream::QDataStream(QByteArray*, int)'

collect2: ld returned 1 exit status

So now i'm thinking of using the mpg123 lib for decoding mp3 files.

I need help integrating the library in Qt. I've never used a pure c++ library in Qt before so i don't have much idea on how to integrate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-10-16 05:11:58

嘿大家!!终于我明白了!

int MP3Player::Init(const char *pFileName)

{

    mpg123_init();

    m_mpgHandle = mpg123_new(0, 0);
    if(mpg123_open(m_mpgHandle, pFileName) != MPG123_OK)
    {
        qFatal("Cannot open %s: %s", pFileName, mpg123_strerror(m_mpgHandle));
        return 0;
    }
}

int MP3Player::Play()

{

    unsigned char *audio;
    int mc;
    size_t bytes;
    qWarning("play_frame");


    static unsigned char* arr = 0;

    /* The first call will not decode anything but return MPG123_NEW_FORMAT! */

    mc = mpg123_decode_frame(m_mpgHandle, &m_framenum, &audio, &bytes);

    if(bytes)
    {

        /* Normal flushing of data, includes buffer decoding. */

        /*This function is my already implemented audio class which uses ALSA to output decoded audio to Sound Card*/
        if (m_audioPlayer.Play(arr,bytes) < (int)bytes) 
        {
            qFatal("Deep trouble! Cannot flush to my output anymore!");
        }

    }
    /* Special actions and errors. */
    if(mc != MPG123_OK)
    {
        if(mc == MPG123_ERR)
        {
            qFatal("...in decoding next frame: %s", mpg123_strerror(m_mpgHandle));
            return CSoundDecoder::EOFStream;

        }
        if(mc == MPG123_DONE)
        {
            return CSoundDecoder::EOFStream;
        }
        if(mc == MPG123_NO_SPACE)
        {
            qFatal("I have not enough output space? I didn't plan for this.");
            return CSoundDecoder::EOFStream;
        }
        if(mc == MPG123_NEW_FORMAT)
        {
            long iFrameRate;
            int encoding;
            mpg123_getformat(m_mpgHandle, &iFrameRate, &m_iChannels, &encoding);

            m_iBytesPerChannel = mpg123_encsize(encoding);

            if (m_iBytesPerChannel == 0)
                qFatal("bytes per channel is 0 !!");

            m_audioPlayer.Init(m_iChannels , iFrameRate , m_iBytesPerChannel);

        }
    }
}

Hey all !! Finally I figured it out !!

int MP3Player::Init(const char *pFileName)

{

    mpg123_init();

    m_mpgHandle = mpg123_new(0, 0);
    if(mpg123_open(m_mpgHandle, pFileName) != MPG123_OK)
    {
        qFatal("Cannot open %s: %s", pFileName, mpg123_strerror(m_mpgHandle));
        return 0;
    }
}

int MP3Player::Play()

{

    unsigned char *audio;
    int mc;
    size_t bytes;
    qWarning("play_frame");


    static unsigned char* arr = 0;

    /* The first call will not decode anything but return MPG123_NEW_FORMAT! */

    mc = mpg123_decode_frame(m_mpgHandle, &m_framenum, &audio, &bytes);

    if(bytes)
    {

        /* Normal flushing of data, includes buffer decoding. */

        /*This function is my already implemented audio class which uses ALSA to output decoded audio to Sound Card*/
        if (m_audioPlayer.Play(arr,bytes) < (int)bytes) 
        {
            qFatal("Deep trouble! Cannot flush to my output anymore!");
        }

    }
    /* Special actions and errors. */
    if(mc != MPG123_OK)
    {
        if(mc == MPG123_ERR)
        {
            qFatal("...in decoding next frame: %s", mpg123_strerror(m_mpgHandle));
            return CSoundDecoder::EOFStream;

        }
        if(mc == MPG123_DONE)
        {
            return CSoundDecoder::EOFStream;
        }
        if(mc == MPG123_NO_SPACE)
        {
            qFatal("I have not enough output space? I didn't plan for this.");
            return CSoundDecoder::EOFStream;
        }
        if(mc == MPG123_NEW_FORMAT)
        {
            long iFrameRate;
            int encoding;
            mpg123_getformat(m_mpgHandle, &iFrameRate, &m_iChannels, &encoding);

            m_iBytesPerChannel = mpg123_encsize(encoding);

            if (m_iBytesPerChannel == 0)
                qFatal("bytes per channel is 0 !!");

            m_audioPlayer.Init(m_iChannels , iFrameRate , m_iBytesPerChannel);

        }
    }
}
很酷不放纵 2024-10-16 05:11:58

为了让 mpg123 与您的 QT 项目配合使用,请尝试以下步骤:

1.下载并安装 mpg123:从将其解压到的文件夹(例如 /home/mpg123-1.13.0/)运行 ./configure,然后运行“sudo make install"

2.如果没有错误,将此行放入您的 *.pro 文件

LIBS += /usr/local/lib/libmpg123.so

3.那么下面的代码应该可以正常运行:

#include "mpg123.h"
#include <QDebug>

void MainWindow::on_pushButton_2_clicked()
{
    const char **decoders = mpg123_decoders();
    while (*decoders != NULL)
    {
        qDebug() << *decoders;
        decoders++;
    }
}

或者您可以通过系统调用调用 mpg123:

system("mpg123 /home/test.mp3"); 

希望这有帮助,问候

In order to get mpg123 working with your QT project you try following steps:

1.download and install mpg123: from the folder where you extracted it to (e.g /home/mpg123-1.13.0/) run ./configure and then "sudo make install"

2.if there are no errors put this line to your *.pro file

LIBS += /usr/local/lib/libmpg123.so

3.then code below should run fine for you:

#include "mpg123.h"
#include <QDebug>

void MainWindow::on_pushButton_2_clicked()
{
    const char **decoders = mpg123_decoders();
    while (*decoders != NULL)
    {
        qDebug() << *decoders;
        decoders++;
    }
}

alternatively you can call mpg123 via system call:

system("mpg123 /home/test.mp3"); 

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文