1

我想在我的嵌入式 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++ 库,所以我对如何集成它不太了解。

4

2 回答 2

1

大家好 !!最后我想通了!!

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);

        }
    }
}
于 2010-12-28T06:10:05.663 回答
0

为了让 mpg123 与您的 QT 项目一起工作,您可以尝试以下步骤:

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

2.如果没有错误,把这一行放到你的 *.pro 文件中

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

3.then 下面的代码应该适合你:

#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"); 

希望这会有所帮助,问候

于 2010-12-24T20:31:39.587 回答