直接上代码:
#include <stdio.h>
#include <ao/ao.h>
#include "mpg123.h"
int main(int argc, char *argv[])
{
ao_initialize();
mpg123_handle *mpg123 ;
int iMpg123_error;
if(MPG123_OK != (iMpg123_error = mpg123_init()))
{
printf("failed to init mpg123\n");
return -1;
}
mpg123 = mpg123_new(mpg123_decoders()[0], &iMpg123_error);
if(MPG123_OK != (iMpg123_error = mpg123_open(mpg123,argv[1])))
{
fprintf(stderr,"error in open mp3 file\n");
return -1;
}
int rate,channel,encoding;
mpg123_getformat(mpg123,&rate,&channel,&encoding);
int default_driver = ao_default_driver_id();
ao_sample_format format;
memset(&format,0,sizeof(format));
format.bits = 16;
format.channels = channel;
format.rate = rate;
format.byte_format = AO_FMT_LITTLE;
ao_device *device;
device = ao_open_live(default_driver,&format,NULL);
if(device == NULL)
{
fprintf(stderr,"error opening device.\n");
return 1;
}
short buffer[4096];
int read = 0;
while(1)
{
mpg123_read(mpg123,buffer,4096,&read);
if(read == 0)
break;
ao_play(device,buffer,read);
}
mpg123_close(mpg123);
ao_close(device);
ao_shutdown();
return 0;
}