mpg123 库播放mp3
#include <alloca.h>
#include <alsa/asoundlib.h>
#include <mpg123.h>
int main()
{
//char* path = "/mnt/usb/music/01 You Only Live Once.mp3";
char* path = "./audio.mp3";
snd_pcm_t* pcmHandle;
snd_pcm_hw_params_t* params;
int numChannels, encoding;
unsigned int bitRate;
unsigned char* mp3Buffer; // Stores decoded MP3 audio data
size_t mp3BufferSize;
// init the mpg123 library
mpg123_init();
// create a new mp3 handle
mpg123_handle* mp3Handle = mpg123_new(NULL,NULL);
// Open the MP3 file for decoding
mpg123_open(mp3Handle, path);
// get the bit rate, number of channels, and encoding for the file to be played
mpg123_getformat(mp3Handle, (long*)&bitRate, &numChannels, &encoding);
printf("=== MP3 Info ===\n");
printf("Channels: %d\n", numChannels);
printf("Sample Rate: %d Hz\n", bitRate);
printf("Encoding: 0x%x ", encoding);
// 打印编码类型的详细信息
if (encoding & MPG123_ENC_16) {
printf("(16-bit ");
} else if (encoding & MPG123_ENC_8) {
printf("(8-bit ");
} else if (encoding & MPG123_ENC_24) {
printf("(24-bit ");
} else if (encoding & MPG123_ENC_32) {
printf("(32-bit ");
}
if (encoding & MPG123_ENC_SIGNED) {
printf("signed");
} else {
printf("unsigned");
}
printf(")\n");
// Allocate a buffer for the mp3 data
mp3BufferSize = mpg123_outblock(mp3Handle);
mp3Buffer = (unsigned char*)malloc(mp3BufferSize * sizeof(unsigned char));
// Open the ALSA audio device for playback
snd_pcm_open(&pcmHandle, "default", SND_PCM_STREAM_PLAYBACK, 0);
// configure parameters for PCM output for the hardware
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcmHandle, params);
snd_pcm_hw_params_set_access(pcmHandle, params,SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(pcmHandle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(pcmHandle, params, numChannels);
snd_pcm_hw_params_set_rate_near(pcmHandle, params, &bitRate, 0);
snd_pcm_hw_params(pcmHandle, params);
// decode the mp3 data, store in the mp3Buffer, and play it back
size_t bytesRead = 0;
//while (mpg123_read(mp3Handle, mp3Buffer, mp3BufferSize, &bytesRead) ==MPG123_OK) {
//snd_pcm_prepare(pcmHandle);
//snd_pcm_writei(pcmHandle, mp3Buffer, bytesRead / numChannels /sizeof(short));
//}
int err;
int keepRuning=1;
while(keepRuning){
err = mpg123_read(mp3Handle, mp3Buffer, mp3BufferSize, &bytesRead);
//printf("err:%d,bytesRead:%ld\n",err,bytesRead);
if (err == MPG123_DONE) {
printf("Playback finished\n");
break;
} else if (err == MPG123_NEW_FORMAT) {
printf("New format detected, updating...\n");
// 重新获取格式信息
//mpg123_getformat(mp3Handle, &rate, &channels, &encoding);
// 这里可能需要重新配置音频设备
continue;
} else if (err == MPG123_NEED_MORE) {
// 需要更多数据,继续读取
continue;
} else if (err != MPG123_OK) {
fprintf(stderr, "Error while decoding: %s\n", mpg123_strerror(mp3Handle));
break;
}
if(bytesRead){
snd_pcm_writei(pcmHandle, mp3Buffer, bytesRead / numChannels /sizeof(short));
}
}
// free the mp3 buffer and close/delete handles
free(mp3Buffer);
snd_pcm_close(pcmHandle);
mpg123_close(mp3Handle);
mpg123_delete(mp3Handle);
}
安装库:sudo apt-get install libmpg123
编译:
gcc test01.c -lmpg123 -lasound
参考:https://opencoursehub.cs.sfu.ca/bfraser/grav-cms/cmpt433/links/files/2023-student-howtos/PlayMp3FileWithC-MPEG123.pdf
https://zhuanlan.zhihu.com/p/1914090419475681773
浙公网安备 33010602011771号