题
我想在嵌入式Linux的QT应用中添加对MP3文件播放的支持。
我无法在QT中使用声子。在.pro文件中添加qt += phonon后,它在编译过程中给我以下错误:/USR/lib/gcc/i486-linux-gnu/4.4.4.1/../../../../../../../../../../../../../../../ libphonon.so:未定义的引用`qwidget :: x11event(_xevent*)'
/USR/lib/gcc/i486-linux-gnu/4.4.4.1/../../../../../../../../../lib/libphonon.so:undefined引用`qdatastream :: qdatastream :: qdatastream(qbytearray*,int)'''
收集2:LD返回1退出状态
因此,现在我正在考虑将MPG123 LIB用于解码MP3文件。
我需要帮助将图书馆集成到QT中。我以前从未在QT中使用过纯C ++库,因此我对如何集成它没有太多想法。
解决方案
大家好 !!终于我弄清楚了!
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);
}
}
}
其他提示
为了使MPG123与您的QT项目一起工作,您可以尝试以下步骤:
1.下载并安装mpg123:从将其提取到(例如/home/mpg123-1.13.0/)的文件夹中。
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");
希望这会有所帮助
不隶属于 StackOverflow