带Qt的mpg123库

时间:2010-12-24 07:26:38

标签: qt mp3 phonon

我希望在我的嵌入式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 ++库,因此我对如何集成它没有太多想法。

2 个答案:

答案 0 :(得分: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);

        }
    }
}

答案 1 :(得分:0)

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

1.download并安装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"); 

希望这有帮助,尊重