使用libmpeg解码mp3格式文件
mp3格式的数据是通过udp循环从硬件发送过来的
#define BUFF_SIZE 1024
//定义线程锁
static std::mutex decodelock;
std::condition_variable dataAvailable;
//mpg123的初始化mpg123_init();int err;mh = mpg123_new(NULL, &err);if (mh == NULL) {qDebug() << "Error initializing mpg123: " << mpg123_plain_strerror(err);return;}//设置参数mpg123_param(mh,MPG123_ADD_FLAGS,MPG123_FORCE_STEREO,100);mpg123_open_feed(mh);
void AudioCode::MP32PCM()
{int buff_size = BUFF_SIZE;unsigned char *buffer = (unsigned char *)malloc(buff_size);size_t frame_offset = 0;qInfo() << "start decode";while (che) {if(mp3DataAll.length() > BUFF_SIZE){std::unique_lock<std::mutex> lock(decodelock);dataAvailable.wait(lock);QByteArray array = mp3DataAll.left(BUFF_SIZE);mp3DataAll.remove(BUFF_SIZE);if (mpg123_feed(mh,reinterpret_cast<const unsigned char *>(array.data()), array.size())!= MPG123_OK) {qDebug() << "Error feeding data to mpg123: %s\n" << mpg123_strerror(mh);continue;}while (mpg123_read(mh, buffer, buff_size, &frame_offset) == MPG123_OK) {audioDeviceO->write(reinterpret_cast<const char *>(buffer),buff_size);}}}qInfo() << "end decode";free(buffer);mpg123_close(mh);mpg123_delete(mh);mpg123_exit();return;
}
//udp接收数据
void AudioCode::processDatagrams(){while (socket->hasPendingDatagrams()) {connectState = false;emit sendConnectState(connectState);timer->start(CONNECT_TIME);QByteArray datagram;datagram.resize(socket->pendingDatagramSize());socket->readDatagram(datagram.data(), datagram.size());std::lock_guard<std::mutex> guard(decodelock);mp3DataAll += datagram;dataAvailable.notify_one();if(mp3InFILE.isOpen())mp3InFILE.write(datagram.data(),datagram.size());}
}